Generate code39/isbn barcodes in php with mpdf

By | May 27, 2013

Barcodes

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.

mPDF

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
<html>
<head>
<style>
div
{
	margin:20px;
}
</style>
</head>
	<body>
	<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>
	</body>
</html>
EOT;

//get the mpdf library
include("lib/MPDF54/mpdf.php");
$mpdf = new mPDF('utf-8','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML($html);
//render the pdf on the browser
$mpdf->Output();

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.

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 code39/isbn barcodes in php with mpdf
  1. Aatmik

    This doesn’t work, as new libraries have been updated on the Publisher’s Website.

    Kindly share the link to download the zip of Library, you are using!

Leave a Reply

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