Generate code39/isbn barcodes in php with mpdf


Barcodes are used in various business class applications to print information in encoded format such that it can be read later by barcode reading devices. Common places to see barcodes are product covers in shops, membership cards and documents like invoices etc. As php is now increasingly being used to develop such applications, barcodes need to be printed from php.

There are lots of free libraries for php that can generate barcodes in either image or pdf format. Barcodes should always be printed in pdf format. PDFs are more print friendly since they have absolute dimensions with defined margin/padding etc, therefore printing barcodes in pdf is most accurate. There are many free pdf generation libraries for php that support barcode generation on the fly. Some of them are mpdf, tcpdf etc.

In this article we shall see how to use some of the pdf libraries to generate barcodes quickly and easily.


The very first library that we are going to try is the mpdf library. It is an excellent library for generating pdfs in php made with pure php. Although it is a more of a pdf generation library, it supports many barcode formats as well. It is one of the most popular and easy to use pdf library for php. It is also documented very well.

Since mpdf intends to convert html into pdf, it has special html style tag 'barcode' to print barcodes. This is much easier compared to calling functions with parameters. Here is a quick example.

//any errors would spoil the pdf
error_reporting(~E_NOTICE & ~E_WARNING);
$code = '9865457216';

//prepare the html
$html = <<<EOT
	<br /><br />
	<div> Code39 : <barcode code="$code" type="C39" size="0.5" height="2.0" /></div>
	<div> ISBN : <barcode code="$code" type="isbn" size="0.5" height="2.0" /></div>
	<div> Code93 : <barcode code="$code" type="c93" size="0.5" height="2.0" /></div>

//get the mpdf library
$mpdf = new mPDF('utf-8','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 
$mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list
//render the pdf on the browser

In the above example barcodes are generated using simple and short barcode tags. Check the
documentation for a complete list of barcode types that mpdf supports.

Last Updated On : 27th May 2013

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at [email protected]. Or find him on

Leave a comment