How to Generate PDF file using PHP

18 comments
It is very important to generate PDF documents in the corporte world. E-Commerce applications developed in PHP provide this functionlity. So I decided to write a tutorial on 'How to Generate PDF file using PHP'. The script uses a class file 'fpdf.php' which is distributed for free use (that's why I love Open Source :)). A separate folder called 'fonts' is there to generate all the fonts. It is kept in the same directory where the main file and 'fpdf.php' is kept. All next is simple PHP programming that can be understood with little brain teasing. Lets see it working.




Live Demo Download Script

PHP Code

<?php
include "../connect.php";
require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage(); //Create new PDF page

$pdf->SetFont('Arial','B',10); //Set the base font & size

//Cell function gets two parameters:Width & Height of the Cell
$pdf->Cell(50,3,"Cool PHP to PDF Tutorial by WebSpeaks.in");
$pdf->Ln();//Creates a new line, blank here
$pdf->Ln();

$pdf->SetFont('Arial','B',6);
$pdf->Cell(10,5,"Sr.no.");
$pdf->Cell(350,5,"Message");
$pdf->Ln();
$pdf->Cell(450,3,"--------------------------------------------------------------------------");

 // Get data records from table.
 $result=mysql_query("select * from records order by msg_id");
 while($row=mysql_fetch_array($result))
 {
  $pdf->Cell(10,5,"{$row['msg_id']}");
  $pdf->MultiCell(350,5,"{$row['message']}");//Necessary for generating a new line.
 }
$pdf->Output();//Finally shows the output.

?>

18 comments

i have been using fpdf, pdflib for years, and i must say they're just hard to maintain given the nature of complex applications. then i found out wkhtmltopdf, which gives you more flexibility and style HTML + CSS can offer.

ColdFusion's version:


// content goes here

Try that again. ColdFusion's version:

<cfdocument format="PDF">
// content goes here
</cfdocument>

Hey Andy!
You can write complete code of Cold Fusion as I am not aware of that.
Thanks for reviews!

I'm not sure exactly what the PHP code is trying to accomplish above, but here's a quick mock of what it would look like using ColdFusion (as spaghetti code, which isn't necessarily the best practice, but this will demonstrate nicely)...

<!--- Database query would go in a class (CFC) or generated by an ORM tool --->
<cfquery name="result">
select * from records order by msg_id
</cfquery>

<!--- Send PDF data to browser --->
<cfdocument format="pdf">
<html>
<head>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
.message { font-size: 6pt; }
</style>
</head>

<body>

<h1>Cool <del>PHP</del> <ins>ColdFusion</ins> to PDF Tutorial by WebsSpeaks.in</h1>

<div class="message">
<cfloop query="result">
<p>#result.message#</p>
</cfloop>
</div>

</body>
</html>
</cfdocument>

Hopefully this captures the spirit of what you were trying to do in PHP. As you can see, generating PDFs in CF is a matter of using and plain vanilla HTML/CSS in the document body. Easy, peasy, lemon squeezy!

I should have used non-breaking spaces above to indent the code instead of regular spaces. Sorry, hope it still comes across clearly.

You should really try Smart PDF Creator. It lets you create PDF documents with a single click. http://www.smartpdfcreator.com

Hi,

Why first record missing in PDF

Srinivas Tamada
http://9lessons.info

Hi,
Great article,It is very useful for me.but one think is when we get data from the table the first record is skip.the output start from the message id 2.whats the problem.

Thanks in advanced......
Please give reply ASPS.

How to insert image using this script

Dear Friends I have confusion to under stand how to fetch record on pdf page is there any dude who help me to send the best way

Dear Friends I have confusion to under stand how to fetch record on pdf page is there any dude who help me to send the best way

i am very new to php, i tried the above code and it gives below error message:

Failed opening required 'fpdf.php' , pls let m know how do i solve this,

I work with www.pdfservices.net long ago I stopped working with classes because the results are not comparable

I got an error
FPDF error: Could not include font metric file

How to solve this issue.

I download script code but not work in that header & connect file not include.

I download this script but ion that not header file & connect file so error display for that.
its not good source for pdf.

How to fetch data using where clause in pdf file

We would love to hear from you...

back to top