PHP Script – Create PDF With FPDF

FPDF is one of powerful and free PHP library for you to create PDF pages or documents on the fly. The library is well documented is easy to use for you who know PHP. PDF pages can be used to generate e-Books, online web reporting and so on. In this topic, we will show you how to create PDF pages using this powerful library.

Download FPDF Class Library

As the first step, you need to download FPDF Class Library. FPDF Class Library can be downloaded for free at Copy the package into your project folder. Please read the installation guide to know how to place this package.

Source Code

    //By    : Xhanch Studio
    //URL   :

    //define FPDF font path
    define('FPDF_FONTPATH', 'fpdf/font/');
    //include FPDF class library file
    require_once 'fpdf/fpdf.php';

    //create an instance of FPDF
    //The first parameter should be paper orientation
    //P refers to potrait and L refers to Letter
    //The second parameter should be measuring-unit used
    //Available units: cm, mm, in and pt
    //The last parameter should be paper type
    //Availale paper types: A4, Letter, and Legal
    $pdf = new FPDF('P', 'cm', 'A4');

    //Init PDF creation

    //Add a new PDF blank page

    //You may set the margin (optional)
    //The first param is the left margin
    //The second param is the top margin
    //The third param is the right margin
    //The fourth param is the bottom margin

    //Set font
    //The first param should be the font family
    //Available fonts: courier, helvetica, times
    //The second param should be the font style
    //Available style: B(Bold), U(Underline) and I(Italic)
    //The third param should be the font size
    $pdf->SetFont('Arial', 'BUI', 21);

    //Set font color
    //Color is represent by RGB
    //The first param is Red color index
    //The second param is Green color index
    //The third param is Blue color index

    //Print a text on the page
    //The first param should be the width of text area
    //0 value will make the FPDF automate the width
    //The second param is the height
    //The third param is the text
    $pdf->Cell(0, 2, 'Hello there!');

    //Print a line break
    //The param is the height of line spacing
    //If you leave it empty, it will use default size

    //Set text alignment and bordering
    //The fourth param is the size of border
    //0 means borderless
    //The fifth param is the size of line spacing
    //0 means no spacing after the text is printed
    //The last param is the text alignment
    //C(Center), L(Left), R(Right)
    $pdf->Cell(0, 2, 'Hello there!',0, 0, 'C');


    //You may re-define the font style as you wish
    $pdf->SetFont('Arial', 'I', 12);

    //Print a multiline/paragraph text
    $text = '
        this is a long paragraph. this is a long paragraph.
        this is a long paragraph. this is a long paragraph.
        this is a long paragraph. this is a long paragraph.
        this is a long paragraph. this is a long paragraph.
        this is a long paragraph. this is a long paragraph.
        this is a long paragraph. this is a long paragraph.
    //The first param should be the width of text area
    //0 value will make the FPDF automate the width
    //The second param is the line spacing for this paragraph
    //The third param is the text
    //You may add more param for multicell
    //The fourth param is the border width
    //The fifth param is the alignment
    //e.g: $pdf->MultiCell(8,1,$text,1,'L');


    $pdf->SetFont('Arial', 'U', 8);

    //Add a link
    //The first param is the line height
    //The second param is the link text
    //The last param is the link URL
    $pdf->Write(1, 'FPDF Example by Xhanch', '');

    //Adding image
    //The first param is the file path
    //Allowed type: jpg, jpeg, gif and png
    //The second param is the x coordinate of the page
    //The third param is the y coordinate of the page
    //The fourth param is the width of the image
    //The fifth param is the height of the image
    $pdf->Image('', 1, 10, 5.8, 1.5);

    //Creating table
    //Table can be created by using Cell function
    //We just need to set the cell border


    //Data for the table
    $header = array('Col A', 'Col B', 'Col C');
    $data = array(
        array('A1', 'B1', 'C1'),
        array('A2', 'B2', 'C2'),
        array('A2', 'B2', 'C2')

    //Simple table
    foreach($header as $col)
    $pdf->Cell(2, 0.5, $col, 1);
    foreach($data as $row){
        foreach($row as $col)
            $pdf->Cell(2, 0.5, $col, 1);


    //Improved table
    //Set Column widths
    foreach($data as $row){


    //Colored table
    foreach($data as $row){

    //Displaying PDF page

    //If you want to force the viewer to download
    //the PDF file use the following command instead
    //of $pdf->Output()
    //$pdf->Output('file_name.pdf', 'D');

Code Result

Click here to view the PDF generated by the above PHP script.

Incoming Search Terms

fpdf line spacing, fpdf set bottom margin, fpdf bottom margin, fpdf line height, fpdf font color, fpdf letter spacing, fpdf border width, fpdf margin bottom, fpdf page border, fpdf set font color, fpdf textarea, fpdf php free download, fpdf paragraph, fpdf setmargins, fpdf margin, $pdf->Ln();, $pdf->Ln, fpdf multicell line height, FPDF wordpress, fpdf border color, fpdf spacing, fpdf set line height, $pdf->ln(), fpdf fontpath, fpdf script, set font color fpdf, fpdf multiline, fpdf create table, setmargins fpdf, fpdf cell margin, set bottom margin fpdf, fpdf set margin, $pdf->SetFont color, fpdf multiline in a cell, fpdf setfont color, margin bottom fpdf, mpdf header margin, pdf ln, fpdf cell border width, wordpress FPDF, textarea in fpdf, fpdf examples, fpdf underline, fpdf lineheight, font path fpdf, fpdf top margin, fpdf character spacing, fpdf font spacing, fpdf SetFontColor, fpdf improved table, fpdf font path, fpdf php, php code to generate pdf, fpdf designer, fpdf scripts, fpdf php code, setwidths fpdf, fpdf setmargins example, fpdf php source code, create pdf php script, fpdf cell multiline, dowload fpdf, pdf setfont color, fpdf bordercolor, fpdf text area, letter spacing fpdf, bottom margin fpdf, fpdf generate pdf table, fpdf setmargin, script fpdf, page border in fpdf, fpdf multiline cell, class fpdf_table php download, fpdf margins, FPDF border, fpdf set margin bottom, fpdf multicell height, set font color in fpdf, $pdf->Ln(, php pdf ln, set bottom margin in fpdf, margin fpdf, fpdf setline, fpdf setfont, fpdf ImprovedTable, border in fpdf, pdf SetFontColor, fpdf ln, fpdf multicell, fpdf line-height, fpdf page margins, create table in fpdf, pdf->Ln, php code for pdf creation, FPDF SET PAPER, drupal FPDF, php $pdf text align, php script create pdf, php to pdf script, fpdf paragraph spacing, Xhanch, Xhanch Studio

Posted on 2010-02-12 by Susanto BSc in Free Script, PHP Script
Free, Fpdf Table, Fpdf, Php Pdf Tutorial, Report, Tutorial, Web Reporting, Reporting, Script, Pdf Generator, Class, On The Fly, Php, Php Pdf, Generator, Pdf, Document, Code, Xhanch, Xhanch Studio

Tagged as: , , , , , , , , , , , , , , , , ,

4 Responses to “PHP Script – Create PDF With FPDF”

  1. alaa says:

    i want put html script and convert it to pdf in the fpdf class

    i dont can work it

    help me please

  2. Andrei says:

    Nice example!
    very helpful code for me!

Leave a Reply

You must be logged in to post a comment.