Search code examples
phpmpdf

Mpdf Barcode size is too big. want to fix height and width


I am trying to create barcode using mpdf. while creating, its automatically changing height and width. Please give me solution to fix height and width even though characters are different.

<barcode code="abcdefghijklmnopqrstuvwxyz1234" type="C39E+" 
class="barcode" size="3" height="3" />

want to generate 5 barcodes on a page with equal space.

page size 50*100mm.


Solution

  • According to mpdf documentation and examples provided when you download the scripts from their website: http://www.mpdf1.com/mpdf/index.php?page=Download

    If you have a look at their barcode example example37_barcodes.php (examples/example37_barcodes.php), if you want to modify the size for the C39E+ barcode type you will have to do this:

    These barcodes are all of variable length depending on the code entered. There is no recommended maximum size for any of these specs, but all recommend a minimum X-dimension (width of narrowest bar) as 7.5mil (=0.19mm). The default used here is twice the minimum i.e. X-dim = 0.38mm.

    The specifications give a minimum height of 15% of the barcode length (which can be variable). The bar height in mPDF is set to a default value of 10mm. 'size' will scale the barcode in both dimensions. mPDF will accept any number, but bear in mind that size="0.5" will set the bar width to the minimum. The 'height' attribute further allows scaling - this factor is applied to already scaled barcode. Thus size="2" height="0.5" will give a barcode twice the default width (X-dim=0.76mm) and at the default height set in mPDF i.e. 10mm.

    The results may vary according to the value passed to the barcode class, so I think if you want a fixed width and height you should try an external library that creates an barcode image file with the desired size or forcing the barcode to adjust to a fixed row or div in a table.

    I had the same problem and tried the following using an external library passing values to a function:

    $codigoBarras = "<img src='barcode/script/html/image.php?code=code128&o=1&t=12&r=1&text=$productoID&f1=Arial.ttf&f2=0&a1=A&a2=' width='110' height='25'>";
    

    image.php file:

    <?php
    if(isset($_GET['code']) && isset($_GET['t']) && isset($_GET['r']) && isset($_GET['text']) && isset($_GET['f1']) && isset($_GET['f2']) && isset($_GET['o']) && isset($_GET['a1']) && isset($_GET['a2'])){
        define('IN_CB',true);
        require('config.php');
        require($class_dir.'/index.php');
        require($class_dir.'/FColor.php');
        require($class_dir.'/BarCode.php');
        require($class_dir.'/FDrawing.php');
        require($class_dir.'/Font.php');
        if(include($class_dir.'/'.$_GET['code'].'.barcode.php')){
            if($_GET['f1'] !== '0' && intval($_GET['f2']) >= 1){
                $font = new Font($class_dir.'/font/'.$_GET['f1'], intval($_GET['f2']));
            } else {
                $font = 0;
            }
            $color_black = new FColor(0,0,0);
            $color_white = new FColor(255,255,255);
            if(!empty($_GET['a2']))
                $code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font,$_GET['a1'],$_GET['a2']);
            elseif(!empty($_GET['a1']))
                $code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font,$_GET['a1']);
            else
                $code_generated =& new $_GET['code']($_GET['t'],$color_black,$color_white,$_GET['r'],$_GET['text'],$font);
            $drawing =& new FDrawing('',$color_white);
            $drawing->add_barcode($code_generated);
            $drawing->draw_all();
            $drawing->finish(intval($_GET['o']));
        }
        else{
            header('Content: image/png');
            readfile('error.png');
        }
    }
    else{
        header('Content: image/png');
        readfile('error.png');
    }
    ?>