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":

http://www.elated.com/articles/create-nice-looking-pdfs-php-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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
29-Sep-10 08:31
TCPDF by Nicola Asuni at http://www.tcpdf.org is much more powerful and constantly developed than FPDF

--
interscienze
06-Jan-11 07:13
hi,

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);
$pdf->tbSetTableType($table_default_table_type);
$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;

$pdf->tbSetHeaderType($aSimpleHeader1);
$pdf->tbDrawHeader();

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

$pdf->tbSetDataType($aDataType1);

while($rowactivity1=mysql_fetch_array($activ_result1,MYSQL_ASSOC))
{
$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;
$pdf->tbDrawData($tdata1);
}


if($pdf->GetY() > 0)
{
$pdf->SetXY($pdf->GetX(),$pdf->GetY()+10);
}
$pdf->tbOuputData();



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

1. Please post this in a new topic:

http://www.elated.com/forums/authoring-and-programming/topic/new/

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

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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).

--
http://web.soothed.com.au/
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 www.skateandwin.com 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 http://pear.php.net/manual/en/package.mail.mail-mime.addattachment.php to attach the PDF to the message and send it.

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
07-Oct-11 19:47
I'm using the following code:


$pdf->Output("reportname.pdf","I");


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:


$pdf->Output("reportname.pdf","D");


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
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
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
jomhadonald@yahoo.ca
11-Feb-13 02:23
@jomhadonald: What's on your line 88?

--
Matt Doyle, Elated
3rd Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated