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

How to add barcode to PDF created with PHP FPDF

97 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.

97 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
    Replies
    1. IEEE Project Domain management in software engineering is distinct from traditional project deveopment in that software projects have a unique lifecycle process that requires multiple rounds of testing, updating, and faculty feedback. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project. The methods used in a IEEE DOmain Project systems development life cycle strategy Project Centers in Chennai For CSE provide clearly defined phases of work to plan, design, test, deploy, and maintain information systems.


      This is enough for me. I want to write software that anyone can use, and virtually everyone who has an internet connected device with a screen can use apps written in JavaScript. JavaScript Training in Chennai JavaScript was used for little more than mouse hover animations and little calculations to make static websites feel more interactive. Let’s assume 90% of all websites using JavaScript use it in a trivial way. That still leaves 150 million substantial JavaScript Training in Chennai JavaScript applications.

      Delete
    2. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. <Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cseAngular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  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. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

    angularjs Training in chennai
    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    ReplyDelete
  8. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    AWS Online Training | Online AWS Certification Course - Gangboard
    AWS Training in Chennai | AWS Training Institute in Chennai Velachery, Tambaram, OMR
    AWS Training in Bangalore |Best AWS Training Institute in BTM ,Marathahalli

    ReplyDelete
  9. 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
  10. Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.
    AWS Training in Chennai |Best Amazon Web Services Training in Chennai
    AWS Training in Rajaji Nagar | Amazon Web Services Training in Rajaji Nagar

    Best AWS Training Institute in BTM Layout Bangalore ,AWS Coursesin BTM

    ReplyDelete
  11. 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
  12. 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
  13. 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

  14. 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
  15. 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
  16. 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
  17. 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
  18. Your each post is so valuable and easy to understand. Thanks for your time and guide..!

    ReplyDelete
  19. 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
  20. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Python Training in Electronic City

    ReplyDelete
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. Enroll today to get free access to our live demo session which is a great opportunity to interact with the trainer directly which is a placement based Salesforce training India with job placement and certification . Get salesforce training in affordable cost from a best computer institute.

    ReplyDelete
  28. 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
  29. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective.keep it up
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  30. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  31. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  32. 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
  33. 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
  34. Did you want to set your career towards Amazon Web Services? Then Infycle is with you to make this into your life. Infycle Technologies gives the combined and best Big AWS Training in Chennai, along with the 100% hands-on training guided by professional teachers in the field. In addition to this, the interviews for the placement will be guided to the candidates, so that, they can face the interviews without struggles. Apart from all, the candidates will be placed in the top MNC's with a great salary package. To get it all, call 7502633633 and make this happen for your happy life.Best AWS Training in Chennai

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

    ReplyDelete
  36. Thanks for posting the best information and the blog is very important.python course in Bangalore

    ReplyDelete
  37. Really awesome blog!!! I finally found great post here.I really enjoyed reading this article. Thanks for sharing your innovative ideas to our vision. your writing style is simply awesome with useful information. Very informative, Excellent work! I will get back here. . AWS Course in Chennai

    ReplyDelete
  38. Get the Best Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Python, Digital Marketing, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.Get Digital Marketing Training in Chennai | Infycle Technologies

    ReplyDelete
  39. 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
  40. Grab Data Science Certification in Chennai for skyrocketing your career with Infycle Technologies, the best Software Training & Placement institutes in and around Chennai. In addition to the Certification, Infycle also gives the best placement training for personality tests, interview preparation, and mock interviews for leveling up the candidate's grades to a professional level.

    ReplyDelete
  41. 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
  42. Smart move for your career is Choosing to do Oracle Course in Chennai at Infycle!! Do you know why this name is chosen for Infycle. Infycle where the place we offered Infinity of Oracle.
    Yes!!! But not only Oracle, More than 20+ courses are offered here 5000+ students are placed in top MNC’s Company with good salary packages. For admission 7502633633.Oracle Course in Chennai | Infycle Technologies

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

    ReplyDelete
  44. İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:

    1- takipçi satın al

    2- takipçi satın al

    3- takipçi satın al

    ReplyDelete
  45. 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
  46. 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