Generate qrcode and pdf417 barcodes in php with tcpdf

By | February 14, 2018

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


// create new PDF document

//set image scale factor

// add a page

// 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('', 'QRCODE,L', 20, 30, 50, 50, $style, 'N');
$pdf->Text(20, 25, 'QRCODE L');

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

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

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

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

// DATAMATRIX (ISO/IEC 16022:2006)
$pdf->write2DBarcode('', '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('', '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('', '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.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

One Comment

Generate qrcode and pdf417 barcodes in php with tcpdf

Leave a Reply

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