Programming, tutorials, mechatronics, operating systems, and other tech stuff

How to add barcode to PDF created with PHP FPDF

52 comments
This time, i'm about to explain how to add barcode to your generated pdf file. This tutorial requires you to understand the basics how to make / generate pdf file in PHP using FPDF library beforehand. So please read Generate Printable Invoice in PHP using FPDF library if you haven't done so.

Why and what is barcode?

Barcode is an optical, machine-readable numeric data consisting of series of black and white bars with varying thickness. Commonly used in label which requires fast-reading done by machine or computer. Barcode can minimize user's manual input so instead of showing only numeric id on your, let's say, invoice. You can also provide barcoded id. So the user, especially in fast-paced transaction environment can just scan it using barcode reader instead of manual input with keyboard. And here's how to add barcode to your generated PDF file in PHP FPDF library.

Barcode support for PHP FPDF library.

There are no built-in barcode support in PHP FPDF library. However, with a custom class, we can add a barcode support. Here i have prepared a barcode class in my github repository you can download right away here: https://github.com/gemul/fpdf-barcode.
There are a lot of barcode standard format, but here we will only use 2 format. EAN13, and UPC_A format which is a commonly used barcode standard.
EAN stands for European Article Number which is a barcode symbology standard and numbering system used in global trade to identify a specific retail product type, in a specific packaging configuration, from a specific manufacturer. While the UPC stands for Universal Product Code which is also a barcode symbology that is widely used in the United States, Canada, United Kingdom, Australia, New Zealand, in Europe and other countries for tracking trade items in stores. Both can hold 12 digit of integer (last digit in EAN13 is a check number so you can only keep 12 digit). For more explanation about barcode standard, you can look it up in wikipedia.

Using custom PHP FPDF class to make a barcode.

First, you need the PHP FPDF library, here i used  FPDF version 1.7 which you can download here.
Then download or copy-paste the fphp_barcode.php file from my repository to your local webserver.
Open the file and make sure that the second line refer to your fpdf.php file which comes with your fpdf library. On my case, the folder structure looks like this:
\htdocs\pdf\
         |-fpdf\
         |  |-fpdf.php
         |-fpdf_barcode.php
 So the "require" path should look like this:

require('fpdf/fpdf.php');

Next, make a new file in the same directory as your fpdf_barcode.php file named barcode-test.php then copy this script into it.

<?php
require('fpdf_barcode.php');

$pdf = new PDF_BARCODE('P','mm','A4');

$pdf->AddPage();

//EAN13 test
$pdf->EAN13(10,10,'123456789012',5,0.5,9);
$pdf->EAN13(10,20,'123456789012',5,0.35,9);
$pdf->EAN13(10,30,'123456789012',10,0.35,9);

//UPC_A test
$pdf->UPC_A(100,10,'123456789012',5,0.5,9);
$pdf->UPC_A(100,20,'123456789012',5,0.35,9);
$pdf->UPC_A(100,30,'123456789012',10,0.35,9);

$pdf->Output();

Save it then see the result in your browser. If nothing goes wrong, it should be like this:

Now, what does the code above do?
First line, we include the fpdf_barcode.php file, not the usual fpdf.php file. We're using a custom class here.
Next, make a PDF_BARCODE object, not FPDF object. With the same parameter as usual. Then add a page. And at the last line, don't forget to output the result. (Please read this fpdf basic if you haven't done so).
With these lines, you are properly made a single page blank PDF file.

<?php
//call the fpdf_barcode.php, not the usual fpdf.php. We're using a custom class here.
require('fpdf_barcode.php'); 

//define a PDF_BARCODE object, not FPDF object. With portrait orientation, milimeters unit, and A4 size.
$pdf = new PDF_BARCODE('P','mm','A4');

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

//the content goes here

//Output the result
$pdf->Output();

In the pdf_barcode class, there are 2 methods called EAN13 and UPC_A. As the name implies, the former is to make a EAN13 barcode and the later is for UPC_A.

For EAN13, use :
$pdf->EAN13(float x, float y, string barcode [, float h [, float w[, int font_size]]])

And For UPC_A :
$pdf->UPC_A(float x, float y, string barcode [, float h [, float w[, int font_size]]])

x is the abscissa of barcode. Which means the horizontal position of the top-left corner of the barcode.
y is the ordinate of barcode. The vertical position of the top-left corner of the barcode.
barcode is the value of barcode, consists of 12 digits integer.
h is the height of barcode. Default value is 16.
w is the width of a bar. Default value is 0.35.
font_size is the font size of the number below barcode.

So, this line :

$pdf->EAN13(10,10,'123456789012',5,0.5,9);
Means that we make an EAN13 Barcode at position of 10mm from left and 10mm from top of the page. The barcode contains 123456789012 and the height is 5 and the bar width (not the whole barcode width) is 0.5. And the text which says the integer value below the barcode have a size of 9 points.

If by any chance you're still confused after following this tutorial, please do enjoy this video-version of this tutorial. And if you want the source code, feel free download it from the link provided in the description or ask me in the comment section of aforementioned video.

52 comments :

  1. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.

    java training in chennai

    java training in bangalore

    java online training

    java training in pune

    ReplyDelete
  2. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
    python training in chennai

    python training in bangalore

    python online training

    python training in pune

    ReplyDelete
  3. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    python training in chennai

    python training in bangalore

    python online training

    python training in pune

    ReplyDelete
  4. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data science training in kalyan nagar
    Data Science with Python training in chenni

    ReplyDelete
  5. Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me 
    python training in rajajinagar
    Python training in btm
    Python training in usa

    ReplyDelete
  6. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Devops training in sholinganallur

    ReplyDelete
  7. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    ReplyDelete
  8. You may need to take a gander at a few connections and test a program or two to locate a satisfactory free program. https://www.altoconvertpngtopdf.com/faq

    ReplyDelete
  9. It’s appropriate time to make some plans for the future and it’s time to be happy. I have read this post and if I could I wish to suggest you some interesting things or tips. Maybe you could write next articles referring to this article. I wish to read more things about it! click here

    ReplyDelete
  10. This empowers watchers to peruse them on a PC screen yet can't be reproduced by a business printer - to protect the copyright. https://altopdf.com/blog/how-to-merge-pdfs-documents-in-a-matter-of-minutes

    ReplyDelete

  11. Very nice post here and thanks for it .I always like and such a super contents of these post.
    Excellent and very cool idea and great content of different kinds of the valuable information's.
    data science online training
    python online training
    uipath online training
    data science with python online training
    rpa online training

    ReplyDelete
  12. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  13. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training


    ReplyDelete
  14. They programmers can switch to the most recent version of the widely used server-side scripting language to improve the load speed of websites without putting extra time and effort. small business web design Toronto

    ReplyDelete
  15. Your each post is so valuable and easy to understand. Thanks for your time and guide..!

    ReplyDelete
  16. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  17. I enjoyed reading your post. I will share it with my other friends as the information is really very useful. Keep sharing your excellent work. Buy Cheap Ean Barcode

    ReplyDelete
  18. To build up a site that is easy to use, amazing, quick stacking, simple to work, savvy, PHP programming services are for the most part liked. CakePHP Application Development

    ReplyDelete
  19. thanks for Sharing such an Awesome information with us.

    I learned World's Trending Technology from certified experts for free of cost.i Got job in decent Top MNC Company with handsome 14 LPA salary, i have learned the World's Trending Technology from Python training in pune experts who know advanced concepts which can helps to solve any type of Real time issues in the field of Python. Really worth trying Freelance seo expert in bangalore

    ReplyDelete
  20. PHP is an "abnormal state" language, implying that it's commonly increasingly human-accommodating (and simpler to learn) than other programming dialects, for example, C, ASP or ASP.net.Why use Laravel

    ReplyDelete
  21. Attend The PMP in Bangalore From ExcelR. Practical PMP in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP in Bangalore.
    ExcelR PMP in Bangalore

    ReplyDelete
  22. This is an incredible rousing article.I am basically satisfied with your great work.You put truly exceptionally accommodating data...  cursos de ti online

    ReplyDelete
  23. In any case, it is essentially used to depict the entirety of the various kinds of PC programs. itools for pc 64 bit cracked

    ReplyDelete
  24. Really good information to show through this blog. I really appreciate you for all the valuable information that you are providing us through your blog.
    visit : Digital Marketing Training in Chennai || Digital Marketing Course in Chennai

    ReplyDelete
  25. Sharing the same interest, Infycle feels so happy to share our detailed information about all these courses with you all! Do check them out
    oracle training in chennai & get to know everything you want to about software trainings

    ReplyDelete
  26. Infycle Technologies, the top software training institute and placement center in Chennai offers the Best Digital Marketing course in Chennai for freshers, students, and tech professionals at the best offers. In addition to Digital Marketing, other in-demand courses such as DevOps, Data Science, Python, Selenium, Big Data, Java, Power BI, Oracle will also be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  27. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read. Best Buy Ean Barcodes Online Services Provider

    ReplyDelete
  28. This post is so usefull and informative.keep updating with more information...
    Computer Systems Testing
    Benefits Of Test Automation

    ReplyDelete
  29. Hi guys, This is a great article. Thanks for sharing this informative information. I will visit your blog regularly for some latest posts. You can travel to India.But first you need an Indian visa.You can never enter India without a visa. I am using India visa website services. This website is of great help and provides fast visa services.

    ReplyDelete
  30. A decent PDF converter ought to give you a simple time utilizing and offer you adaptability so you can work with any document you as per your inclinations. https://pdf-png.com

    ReplyDelete
  31. The inclusion of diverse methodologies across articles reflects the richness of contemporary research. Magazine download

    ReplyDelete