Create Nice-Looking PDFs with PHP and FPDF

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

26-Feb-10 00:00
This is a forum topic for discussing the article "Create Nice-Looking PDFs with PHP and FPDF":

Learn how to use the free FPDF library to produce great-looking PDF documents from within your PHP scripts. Full example included.
21-May-10 11:56
Dear Author,
Thanks for taking the trouble to write and upload this tutorial. I must say it was quite educative to me. It has just made me realize how much I need to own that book.
I am using WAMP(version 2.0) whose PHP is version 5.3,and fpdf version 1.6 and Apache version 2.2.11. I tried the code and I was getting the following error:
FPDF error: Some data has already been output, can't send PDF file.

Please help.

Thanks in advance.
24-May-10 02:44
Hey nnkuba,

It sounds like you're sending some output to the browser before you call $pdf->Output(). This will break the script because $pdf->Output() needs to send HTTP headers, and these can't be sent if any content has already been sent to the browser.

This can happen even if you accidentally have whitespace before your opening <?php tag at the top of your script.

Make sure your script doesn't send anything to the browser before the call to $pdf->Output().

Matt Doyle, Elated
24-May-10 02:58
Thanx Matt for that insight.

I figured out that i had put pdf code in between the html code. So when I removed the html code around the code you supplied and saved the file, it worked. thanx once again. You are formidable.
26-May-10 05:25
@nnkuba: Great stuff.

Matt Doyle, Elated
20-Jul-10 06:53
very nice... but i am having a problem ... i want to show grid on bars .. how can i do this?can you guide me?
21-Jul-10 18:49
@rubina: You could modify the code that draws the y-axis ticks to instead draw lines across the whole chart, eg:

for ( $i=0; $i <= $maxTotal; $i += $chartYStep ) {
$pdf->SetXY( $chartXPos + 7, $chartYPos - 5 - $i / $yScale );
$pdf->Cell( 20, 10, '$' . number_format( $i ), 0, 0, 'R' );
$pdf->Line( $chartXPos + 28, $chartYPos - $i / $yScale, $chartXPos + $chartWidth, $chartYPos - $i / $yScale );

Does that help?

Matt Doyle, Elated
29-Sep-10 08:31
TCPDF by Nicola Asuni at is much more powerful and constantly developed than FPDF

06-Jan-11 07:13

I've created a pdf page using php and its is working fine in my local. But it is not working in the online server. The data content in the server's database is too large when compared to my local database. Here's my code:

$columns = 3; //number of Columns
$pdf->tbInitialize($columns, true, true);
$aSimpleHeader1 = array();
for($i=0; $i<$columns; $i++)
$aSimpleHeader1[$i] = $table_default_header_type;

$aSimpleHeader1[0]['TEXT'] = "Preparedness";
$aSimpleHeader1[0]['WIDTH'] = 112;
$aSimpleHeader1[0]['COLSPAN'] = 2;
$aSimpleHeader1[0]['T_COLOR'] = array(0,0,0);
$aSimpleHeader1[1]['WIDTH'] = 40;
$aSimpleHeader1[2]['WIDTH'] = 40;


$aDataType1 = Array();
for ($i=0; $i<3; $i++)
$aDataType1[$i] = $table_default_data_type;


$activity = $rowactivity1['fld_procedure'];
$districtrole = selectsinglevalue("select fld_roleposition as retv from tbl_rolesmaster where fld_roleid='{$rowactivity1['fld_districtrole']}' and fld_delstatus=0");

$icsrole = selectsinglevalue("select fld_roleposition as retv from tbl_rolesmaster where fld_roleid='{$rowactivity1['fld_icsrole']}' and fld_delstatus=0");

$tdata1 = Array();
$tdata1[0]['TEXT'] = $activity;
$tdata1[1]['TEXT'] = $districtrole;
$tdata1[2]['TEXT'] = $icsrole;

if($pdf->GetY() > 0)

Should i make any correction in database or in source code.
Please give a solution.
Thank in advance.
06-Jan-11 23:32

1. Please post this in a new topic:

2. Please use the <> code button to wrap your code in code tags.

3. "The data content in the server's database is too large when compared to my local database" - if you have a problem with your data then changing the PHP code won't help!


Matt Doyle, Elated
19-Jul-11 06:02
Thanks for the great tutorial. I found it to be a great help with TCPDF basics too (which is based on FPDF but takes it a bit further).

Web design for natural therapists
30-Sep-11 07:55
This is lovely. I used it to make people download their form after registering on my new site but i want to save the created PDF to a file and also send it as attachment to their email. is this possible
04-Oct-11 23:07
@aristosoft: Yes. Use the 'S' option to return the PDF data as a string, then pass the string to to attach the PDF to the message and send it.

Matt Doyle, Elated
07-Oct-11 19:47
I'm using the following code:


But instead of my report appearing inside 'reportname.pdf', it is appearing inside the 'report.php' page which is the name of the file where I placed this line of code.

I don't want to change the code to:


because this forces the file to be downloaded to a computer. The data in my report is sensitive so I don't want a copy of the report remaining in any download folder.

Any suggestions?
12-Oct-11 06:49
@Alex Kos: I don't understand the problem. What do you mean when you say the PDF appears "inside the 'report.php' page"?

It's worth pointing out that most browsers will likely cache the PDF file, even if they're displaying the PDF inline. Some browsers also ignore the 'I" option and download the PDF anyway. It's also easy for the user to save an inline PDF using File > Save As. So there is likely to be a copy of the PDF somewhere on the user's computer, even if you use the "I" option.

Matt Doyle, Elated
01-Jul-12 03:10
I found it very helpful.. thanks a lot
05-Feb-13 16:13
Good day everyone, I have tried the above code to convert a HTML form to PDF, but It shows error both in my local server and the server online. I'm using wamp Version 2.0. In the local server I am having the following error: Parse error: parse error in C:\wamp\www\FPDF\report.php on line 88; while in the server online I have the fillowing error: FPDF error: Image file has no extension and no type was specified:
Please can someone help, I will be very grateful. Thanks
11-Feb-13 02:23
@jomhadonald: What's on your line 88?

Matt Doyle, Elated
26-Jun-13 02:49
Thanks for the useful post,

but how can i add content in to new pdf page when the page exeeds

thanks ,

[Edited by rohith038 on 26-Jun-13 02:50]
26-Jun-13 05:34
exceeds ...........

What exactly?

So long, and thanks for all the fish.
27-Jun-13 06:46
@chrishirst i mean when one page fills i need to move remaining contents to next page .Is there any extra code to be added for that .Currently i cannot view the remaining contents

27-Jun-13 16:47
It handles line breaks based on the paper size you set.

So long, and thanks for all the fish.
28-Jun-13 23:52
thanks man it helped..
11-Sep-14 08:45
Good read! I just want to share this site which i find very useful. It lets upload word and powerpoint document to be converted to PDF. It also let me fill out the text fields, add a variety of check marks, digitally sign the form and even add pictures. It also has the capability to print, fax, share, mail or even save it to your computer. Awesome tool. I was able to get also the form i need through

Link to fake IRS form REMOVED

[Edited by chrishirst on 11-Sep-14 15:14]
12-Sep-14 05:03
Thanks a lot..Keep Posting!
28-Dec-16 11:48
The fpdf library must be installed with composer as detailed at , in the working website directory. The default linux directory for web root is /var/www/html. I wasn't able to view the project file until I had a few laps around my system and the web for about 2 days and finally figured out that I was supposed to actually install the package with composer. Neither did I find this instruction on my virtual jog.
30-Dec-16 11:46
"The default linux directory for web root is /var/www/html."

This actually depends how Apache is setup. and which, if any control panel is in use.
For example:

Plesk will set the document root as /var/www/vhosts/[account-user-name]/httpdocs

cPanel will set /home/[account-user-name]/public_html

So long, and thanks for all the fish.

New posts
Old posts

Follow Elated