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

[PHP FPDF] How to Integrate QR Code in PDF Generation

22 comments
After long time without update, this time, i will post about how to use QR Code in PDF created with PHP FPDF Library.


For those who doesn't know what QR Code is, here's a brief summary. QR Code, invented in 1994 by the Japanese company Denso Wave, is a 2 dimensional version of barcode. And unlike barcode, QR Code are capable to store alphanumeric, symbol, binaries, and kanji. Right now, it's widely used in many application such as product identification, payment, data entries, advertisement, announcement, and many more. In short, it looks like this:
And you can scan it with your smartphone to get information contained in it.

Now, let's get back to the topic.

In my previous tutorial about How to add barcode to PDF created with PHP FPDF, we extend our FPDF class so it could provide barcode generation suport. But this time, we are using an entirely independent library called PHP QR Code by deltalab.
Download the PHP QR Code library here : http://sourceforge.net/projects/phpqrcode/files/

Then extract it to your working directory alongside the fpdf directory.

Then from the browser, try to open that directory to try the demos. If nothing goes wrong, you will be presented by something like this.


This is a general-purpose QR Code generator, which means you can use it in another project other than PDF.

Now, create a new php file in your working directory. Then make a blank pdf page out of it.
Please read Generate Printable Invoice in PHP using FPDF library if you haven't done so.

Add this code in your new file:
<?php
require_once("fpdf17/fpdf.php");

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

$pdf->AddPage();

$pdf->Output();
And the result should be a blank pdf page like this


Before we continue, there are 2 method to insert the QR Code into our PDF File. And both works by using FPDF Image method to insert an image in the pdf.

First method is by using a QR Code generator, second is generating a QR Code image file upon PDF Generation.

Let's try out the former.

Create a QR Code Generator.

Create a new php file, name it qr_generator.php. Then add these 2 simple lines of code in it.
<?php
require_once("phpqrcode/qrlib.php");

QRcode::png($_GET['code']);
Then try to access that file in browser and provide value to code parameter like this:
http://localhost/pdf/qr_generator.php?code=1234567
and you should get something like this
Congratulation, you've just made a general-purpose QR code generator. Now let's embed it into our PDF Generator.
Simply just add an image using FPDF Image method with that url as the image source parameter like this
<?php
require_once("fpdf17/fpdf.php");

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

$pdf->AddPage();

$pdf->Image("http://localhost/pdf/qr_generator.php?code=content here", 10, 10, 20, 20, "png");

$pdf->Output();
and you should get something like this

Just change the code parameter and image position to suit what you need. With this, you can use the qr code generator for your other project. Just make sure to secure your generator with secret key or tokens, i won't explain it here but to help you start, just use a simple IF statement to check if there are specified secret key passed in the GET request and immediately exit the code if it turns out false.

Now let's try the second method.

Create the QR Code upon PDF Generation.

With this method, instead of using separate QR code generator. We are generating the QR image inside our PDF generator script, store the QR image in a directory, then insert the image using FPDF Image method.

First, include the phpqrcode library in your file.
Then create the QR code using QRcode::png method
QRcode::png("coded number here","test.png");
Note that i added second parameter to the method which indicates that the resulted file will be saved as an png file with that name.

Then add that image in the pdf using Image method like usual. So your final code should look like this (I've provided comments to help explain each parts of the code) :
<?php
//include the libraries
require_once("fpdf17/fpdf.php");
require_once("phpqrcode/qrlib.php");

//create a QR Code and save it as a png image file named test.png
QRcode::png("coded number here","test.png");

//pdf stuff
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();

//this is the first method
$pdf->Image("http://localhost/pdf/qr_generator.php?code=content here", 10, 10, 20, 20, "png");

//this is the second method
$pdf->Image("test.png", 40, 10, 20, 20, "png");

//and absolutely don't forget this one
$pdf->Output();

And if nothing goes wrong, you should get something like this:
IMPORTANT NOTICE : In real world practice, it's necessary to use different file name for each PHP session because the png file would be replaced at every request. And also don't forget to remove the file regularly to prevent it from bloating your storage with a lot of unused png file.

Both method have its pros and cons, just pick whichever method suit your need.

Finally, please check out my youtube video here and please kindly subscribe to my channel for more tutorials. https://www.youtube.com/watch?v=XFpuxgpUXgE



22 comments :

  1. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    Amazon Web Services training in Chennai | Amazon Web Services training class in Chennai

    ReplyDelete
  2. It's really worth reading the blog post whcih has useful content. I have recently done CRS Info Solutions which is highly helpful to upgrade my career. What is the Salesforce course training fee in Chennai? Which is the best option from Salesforce training in Chennai? Who offers job oriented Salesforce training in Chennai? Find the best institute for project based Salesforce training in Chennai.

    ReplyDelete
  3. Those strange-looking shape-filled squares you've been seeing lately may not look like much, but they are very likely the next big thing in marketing and branding for U.S. businesses. With a simple snap of a smartphone, that unassuming graphic sends valuable information instantly to potential customers, which for some businesses can mean the difference between a sale or a walk-away. QR Code Creator

    ReplyDelete
  4. Inconsistency in output quality: If the provider {you have chosen|you've chosen|you've selected|you have selected} is inexperienced and lacks consistency, {then it|it|this|that} {might lead to|could trigger|might trigger|may cause} problems {such as|for example|including|like} delayed submission of completed projects, processed files without accuracy and quality, inappropriate assignment of responsibilities, {lack of communication|no communication|poor communication} {and so|and thus|therefore|so} on| While the job profile {might seem|may appear|may seem|might appear} simple {it does|it will|it can|it lets you do} {in fact|actually|in reality|the truth is} {require a|need a|demand a|have to have a} certain {degree of|amount of|level of|a higher level} exactness {and an|as well as an|plus an|with an} eye for detail| My writing {is focused|is concentrated|is targeted|concentrates} {more on|more about|read more about|on} {the industry|the|a|that is a} {and quality of|and excellence of|superiority} work, not the worker| By continues monitoring the hurdles and solving it, {one can|it's possible to|you can|one can possibly} easily {increase the|boost the|raise the|improve the} productivity of business| Decline {in the|within the|inside the|inside} quality of service and delay {in the|within the|inside the|inside} execution and delivery of processes are some {of the|from the|with the|in the} risks involved, {besides the|aside from the|in addition to the|apart from the} risk {to the|towards the|for the|on the} security {of the|from the|with the|in the} data and privacy and cost-related risks| The {service provider|company|supplier|vendor} {should also|also needs to|must also|also need to} volunteer {a variety of|a number of|many different|various} profits concerning formulas {of data|of information|of knowledge|of internet data} transmission, turnaround etc}. { A lot of companies are fine with admitting this, but {others are|other medication is|other people are} {not so|not too|not|less than} sure, primarily {because this|as this|since this|simply because this} may put people {off the|from the|off of the|over} service| Such measures would {keep your|keep the|maintain your|maintain} sensitive documents from falling {into the|in to the|to the|in the} hands of unauthorized personnel| When you outsource {to an|for an|to a|with an} experienced BPO company, {they would|they'd|they might|they will} manage these risks professionally {as well as|in addition to|along with|and also} plan and implement appropriate {strategies to|ways of|ways to|methods to} avoid them in future| Outsourcing data entry is most helpful term {for all|for those|for many|for all those} these organizations| With the help of such information, {you can|you are able to|it is possible to|you'll be able to} {improve on|enhance|make improvements to} customer targeting| If you think {you are|you're|you might be|you happen to be} proficient enough in installing the payment processor {on your|in your|on your own|on the} website {on your|in your|on your own|on the} own, {you should not|you shouldn't|you ought not|it's not necassary to} hesitate doing it}.pdf data entry services

    ReplyDelete
  5. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. cfa level 1 mock exam pdf with answers

    ReplyDelete
  6. { Otherwise, {you can|you are able to|it is possible to|you'll be able to} directly ask Pay - Pal {or other|or any other|or another|and other} {methods to|techniques to|solutions to|ways to} {help you|assist you to|enable you to|allow you to} on phone or through remote desktop software| Some things {to look|to appear|to check|to take a look} out for {are the|would be the|will be the|include the} {availability of|accessibility to|option of|use of} direct {phone numbers|telephone numbers|cell phone numbers|numbers}, instant messaging support, and direct emails| You will never {know exactly|know precisely|understand specifically|specifically} that {what type of|which kind of|what sort of|which} client {you are|you're|you might be|you happen to be} handling {and you have|and you've got|plus you've got|along with} to be prepared {and have|and also have|and possess|and still have} some {bunch of|couple of|few|lot of} {ideas to|suggestions to|tips to|tricks to} {make out|write out|find out|figure out} the solutionspdf data entry services

    ReplyDelete
  7. This unique looks utterly perfect. Every one of plain and simple records are prepared through the help of great number for working experience handy experience. I will be happy it all for a second time considerably. mutuelle generation

    ReplyDelete

  8. Good evening I would like to contact you to see if you can help me with this code since I can implement it in localhost but when uploading it to the web it gives me an error

    ReplyDelete
  9. Online slots (Slot Online) is actually the introduction of a gambling machine. Slot machine As said before above Used to make electronic games referred to as web-based slots, due to the development era, people have turned to gamble with one another by computers. Will provide slot games to make internet gambling games Via the internet network process Which players are able to play through the slot program or will play Slots with the service provider's site Which internet slots games are obtainable in the form of participating in rules. It is similar to playing on a slot machine. Both practical images and sounds are at the same time thrilling as they go to lounge in the casino ever.บาคาร่า
    ufa
    ufabet
    แทงบอล
    แทงบอล
    แทงบอล

    ReplyDelete
  10. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... how to start a payment gateway company

    ReplyDelete
  11. Excellent effort to make this blog more wonderful and attractive. slice invite code

    ReplyDelete
  12. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog. slice invite code

    ReplyDelete
  13. tombola casino review 2021 - ThTopBet.com 우리카지노 계열사 우리카지노 계열사 william hill william hill 카지노 카지노 245Free Odds Shark No Deposit Casino India - 2021 Bonus & Free

    ReplyDelete
  14. buy twitch followers Online games gives you an ultimate gaming experience with a realistic environment, superb graphics and sound clarity. They are absolutely relaxing and are popular among people of all ages.

    ReplyDelete
  15. https://www.visualaidscentre.com/service/eyes-specialist-delhi/ There are some games, simple, yet so innovative that the moment they hit the market, become instant hits. 7 Wonders is one of them. With a load of awards already won, let's see what makes this game unique.

    ReplyDelete
  16. https://www.buyyoutubesubscribers.in/2022/01/10/which-youtuber-has-most-subscribers/ Influencer Marketing, the process of identifying, engaging and supporting the folks who create the promotion which has the most impact on your brand, products, and services. Influencers are likely to be customers because they also testify what you are promoting, or, we could also call them as recommenders of your products and services, because introduce and recommend your product to the crowd or their audiences. So, choosing the right influencer is essential.

    ReplyDelete
  17. https://dynamichealthstaff.com/jobs-in-dubai-for-nurses If you own a small business SEO consultancy services could be just the thing you need to stop sliding into Internet oblivion. How can professional SEO services benefit your small business? This article will tackle this issue. Please read on to find out more information.

    ReplyDelete
  18. https://hostinglelo.in If you're interested in getting started in creating videos for YouTube, but have no idea where to begin, or what to start with, this article will mention the best way to start from choosing the right niche, the best video editing program for beginners, which camera to choose, setting up your YouTube channel and optimizing it, and setting up your video blog, which is basically a normal blog with your videos embedded in it. All the steps are easy to follow and once you grasp the whole concept, you will have a lot of fun! Happy learning!

    ReplyDelete
  19. I really appreciate the time you took to write out this tutorial. I learned a lot from it and will be sure to implement it into my project. Thanks for sharing! Best QR Code Generator

    ReplyDelete
  20. One more element given by a larger number of people of the PDF Converter instruments is that an individual can combine the reports into a solitary record for making the data or the information sensible. PDF to PNG

    ReplyDelete
  21. Its good.
    now, how create qrqode with existing pdf files ?

    ReplyDelete