How to Generate PDF file using PHP

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

include "../connect.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");
$pdf->Ln();//Creates a new line, blank here


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


Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.


21 thoughts on “How to Generate PDF file using PHP

  1. 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.

  2. If you like fpdf, you should try tcpdf ( It offers more functionality than fpdf and is still actively maintained and developed. Switching from fpdf to tcpdf should not be too hard, since most of the code will keep working.

  3. 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

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


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

    <div class=”message”>
    <cfloop query=”result”>


    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!

  4. 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.

  5. 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,

  6. We are a gaggle of volunteers and starting a new scheme in our community.
    Your site provided us with uxeful info to work on. You've done an impressive activity and our
    entire community shall be grateful to you.

Comments are closed.