How to display data from database into PDF pages | PHP FPDF Tutorial #4

How to display data from database into PDF pages | PHP FPDF Tutorial #4


welcome to the fourth chapter of PHP FPDF tutorial series. in this chapter, i’m about to demonstrate how to put data from database into PDF. and how to represent data in printable PDF pages. in this tutorial, we will utilize header and footer from previous chapter. and we will also learn how to set page break, deal with overflowing content. i have prepared some random dummy data which contain list of clients with phone, email, and address. the data is stored in mysql table in database. in order to trigger page break, we need a lot of data, so here i’ve prepared 200 of them. first, we need to make database connection. next, remove unnecessary lines from previous chapter. now we need to make the page and table heading inside Header function so it will be repeated on each pages. here i use SetDrawColor() and SetFillColor() to make it prettier. and add true as Cell’s seventh parameter to apply background color into the cell. let’s leave the footer for later and jump to the content. just as usual, make a database query then iterate the result into cells. as you can see here, the content is automatically broke when reaching end of page. the default page break is 2 centimeter from the bottom of page. to change that, we can use SetAutoPageBreak() method. here, i set it to 15 millimeter. now, as we can see here, the overflowing text is overlap into next cell. there are many options to deal with that. the easiest way is to truncate the content, but it will conceal the rest of the information. i will explain how to deal with overflowing text in another chapter. this time, because the text is only overflowed a little, we can just shrink it to fit the cell. we can utilize GetStringWidth method to see if the text will overflow the cell and let logic do the rest. now let’s make the table looks better. use only left and right border for content’s cell. and finally put dummy cell in footer to close the table. this concludes this chapter of tutorial. thank you for watching. see you in the next chapter. Like and Subscribe for more tutorial.

Danny Hutson

100 thoughts on “How to display data from database into PDF pages | PHP FPDF Tutorial #4

  1. Hey, I designed a form and I'm getting the results in my email..
    but is there any possibility that i can get the results in PDF form ? as an attachment in my mail?

  2. hai, I was try it in my online database,, but not showing anything / blank page in result. help me please..

  3. "Warning: include(helvetica.php): failed to open stream: No such file or directory" this error pops up when I start my project

Leave a Reply

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