Generate qrcode and pdf417 barcodes in php with tcpdf

By | March 10, 2013

Tcpdf is a pdf creation library for php that is written in pure php and works without the need of any external library. It also has the feature to generate 2d barcodes like qrcode and pdf417. The 2d barcodes can store much more information than the 2d ones like code39 and ISBN.

It is recommended to generate barcodes inside pdf so that they are printed very accurately on hard media like paper and then read properly by barcode readers. Tcpdf has a simple function called write2DBarcode to print a 2d barcode inside the pdf doc.

Tcpdf can export the barcode in image formats like png too. The tcpdf class is documented here. For details on the type parameter and supported formats check this page.

Lets take a look at the code that is used to generate the above shown barcodes

require_once('../lib/tcpdf/config/lang/eng.php');
require_once('../lib/tcpdf/tcpdf.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// add a page
$pdf->AddPage();

// set style for barcode
$style = array(
    'border' => 2,
    'vpadding' => 'auto',
    'hpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255)
    'module_width' => 1, // width of a single module in points
    'module_height' => 1 // height of a single module in points
);

// QRCODE,L : QR-CODE Low error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,L', 20, 30, 50, 50, $style, 'N');
$pdf->Text(20, 25, 'QRCODE L');

// QRCODE,M : QR-CODE Medium error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,M', 20, 90, 50, 50, $style, 'N');
$pdf->Text(20, 85, 'QRCODE M');

// QRCODE,Q : QR-CODE Better error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,Q', 20, 150, 50, 50, $style, 'N');
$pdf->Text(20, 145, 'QRCODE Q');

// QRCODE,H : QR-CODE Best error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,H', 20, 210, 50, 50, $style, 'N');
$pdf->Text(20, 205, 'QRCODE H');

// PDF417 (ISO/IEC 15438:2006)
$pdf->write2DBarcode('www.tcpdf.org', 'PDF417', 80, 90, 0, 30, $style, 'N');
$pdf->Text(80, 85, 'PDF417 (ISO/IEC 15438:2006)');


// DATAMATRIX (ISO/IEC 16022:2006)
$pdf->write2DBarcode('http://www.tcpdf.org', 'DATAMATRIX', 80, 150, 50, 50, $style, 'N');
$pdf->Text(80, 145, 'DATAMATRIX (ISO/IEC 16022:2006)');

// new style
$style = array(
    'border' => 2,
    'padding' => 'auto',
    'fgcolor' => array(0,0,255),
    'bgcolor' => array(255,255,64)
);

// QRCODE,H : QR-CODE Best error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,H', 80, 210, 50, 50, $style, 'N');
$pdf->Text(80, 205, 'QRCODE H - COLORED');

// new style
$style = array(
    'border' => false,
    'padding' => 0,
    'fgcolor' => array(128,0,0),
    'bgcolor' => false
);

// QRCODE,H : QR-CODE Best error correction
$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,H', 140, 210, 50, 50, $style, 'N');
$pdf->Text(140, 205, 'QRCODE H - NO PADDING');

//Close and output PDF document
$pdf->Output('example_050.pdf', 'I');





As can be seen, most of the work is done by the function write2dBarcode. It takes the text and barcode type with few additional parameters, and generates the barcode on the pdf page.

Last Updated On : 14th February 2018

Related Post

One thought on “Generate qrcode and pdf417 barcodes in php with tcpdf

Leave a Reply

Your email address will not be published. Required fields are marked *