Tcpdf a pdf generating php class

I needed to generate a pdf dynamically from the website for one of my project. On my search for a good library i came across TCPDF . It is a Free Libre Open Source Software (FLOSS). You can see more about it here. What today i am going to write is to how quickly use tcpdf for your pdf generation need on the fly.

First download tcpdf and extract it.

Upload the extracted tcpdf folder to your site.

now set the variable according to your site in tcpdf/config/tcpdf_config.php file.

Now after you have successfully installed tcpdf. Now you can see examples to generate the pdf in examples folder or follow following steps. I had to generate pdf from html text so i did following.

Create a new php file Let’s call it pdf.php

In that file include the required library files.


require_once('../../tcpdf/config/lang/eng.php');
require_once('../../tcpdf/tcpdf.php');

After that create a new pdf instance


// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

After that set some pdf properties


// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Your name here");
$pdf->SetTitle("Give your pdf a title");
$pdf->SetSubject("set a subject here);
$pdf->SetKeywords("put, keywords, here, seperating, with, commas");

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

Below are some settings for header footer fonts, page breaks, etc


// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

This will initialize your document


//initialize document
$pdf->AliasNbPages();

Adds a page in your pdf document

// add a page
$pdf->AddPage();

Set a desired font here

// set font
$pdf->SetFont("vera", "", 9);

This is the variable containing html content.

$htmlcontent = "Put your html conent here";

This will write html content to your pdf document

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

Finally output the pdf document.

//Close and output PDF document
$pdf->Output();

After you call this page from your web browser. you will get a pdf file with the content as described.

here is full source code


<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Your name here");
$pdf->SetTitle("Give your pdf a title");
$pdf->SetSubject("set a subject here);
$pdf->SetKeywords("put, keywords, here, seperating, with, commas");

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

//initialize document
$pdf->AliasNbPages();

// add a page
$pdf->AddPage();

// ---------------------------------------------------------

// set font
$pdf->SetFont("vera", "", 9);

// create some HTML content
$htmlcontent = "put your html content here";

// output the HTML content
$pdf->writeHTML($htmlcontent, true, 0, true, 0);

// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output();

//============================================================+
// END OF FILE
//============================================================+
?>
This entry was posted in Tutorials and tagged , . Bookmark the permalink.

24 Responses to Tcpdf a pdf generating php class

  1. Lamyt says:

    Hi,

    I want to use some PHP var in the $htmlcontent. But when I print the PDF file, it show me the $htmlcontent HTML code.

    Have you some idea about it?

    (Sorry for my english)

  2. yalamber says:

    can you send me the code or post it in the comment. be sure you are using double quote “” while playing with variables and nor ”.

  3. Lamyt says:

    It was not the ” or ”. It was just the UTF-8 encoding. Before all HTML in a PHP var, I must use the utf8_encode function.

    Thank you for your help and good continuation

  4. saiful says:

    Hi Yalamber,

    I am new in tcpdf. I manage to export the php code to pdf but I have problem when try to get the data from mysql database. It just got blank? Have you ever try to output any data from mysql database?

    Please teach me if you know. Thanks in advance.

    Saiful.

  5. yalamber says:

    @saiful
    hello,
    To output pdf from mysql database.
    Try this:
    1.first be sure you have all the data fetched from database as you would do before.
    2. In above pdf.php file where there is
    $htmlcontent = “put your html content here”;
    replace with:
    $htmlcontent = “$_POST['htmlcontent']“;

    3. Now create a page where you can access the fetched data. let’s call it getpdf.php
    In it connect to your mysql server and select database and fetch the required data.
    Now i suppose you want to output html content.
    So create a form like this:

    <form action="pdf.php" method="post">
    <input type="hidden" name="htmlcontent" value="$row['fetched html content']" />
    <input type="submit" value="GET pdf" />
    </form>

    This is one of the way you can do what you want to. thanks. Comment if you find anything confusing.

  6. saiful says:

    Hi Yalember,

    Thanks for your reply. I try to do as you said but i get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\web\wamp\www\jrk\pdf.php on line 89

    which is $htmlcontent = “$_POST['htmlcontent']“;

    Do you have any idea why?

    Thanks in advance.

  7. saiful says:

    sorry…. one more

    if I do $htmlcontent = “$_POST[htmlcontent]“; with out ”, i manage to get the pdf but empty pdf.

  8. yalamber says:

    Sorry my mistake add following
    $htmlcontent=”{$_POST['htmlcontent']}”;

  9. saiful says:

    thanks yalamber. it works. really milion thanks for your help.

  10. bahrain says:

    dear yalamber,
    i’ve tried this coding but the pdf only contain

    $row[\'fetched html content\']

  11. yalamber says:

    can you send the code you have used to output the pdf. It will help ful to find the error.

  12. bahrain says:

    already sent an email 2 u..

  13. yalamber says:

    in which email did you sent. Send in kiratisaathi@gmail.com

  14. Manoj Kumar says:

    I am unable to config the tcpdf config file, Please guide me.

  15. Muhammad Mohsin Ali says:

    How can I modify footer!!!

    I want to place custom content in footer. It should like:

    ________________________________________________________________
    version 1.0 Application name

    Please guide me …. Where to change and wot to change…

  16. yalamber says:

    @muhammad
    To add custom header and footer in your tcpdf see the following example:
    http://www.tecnick.com/pagefiles/tcpdf/example_003.phps

  17. Muhammad Mohsin Ali says:

    How to set margins in pdf using tcpdf..

    Left and Right margins??

  18. tinhdx says:

    Hi,
    How can create a header and footer using html code

  19. tinhdx says:

    Hi,
    I have a code as follows, and I wish it could be used to tag to print header on the next page. Here, I use the thead tag does not work:
    $tbl = <<<EOD

    A
    XXXX
    XXXX
    XXXX
    XXXX
    XXXX

    B
    XXXX
    XXXX
    XXXX
    XXXX
    XXXX

    1.
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    XXXXXXXX
    XXXXXXXX
    XXXX
    XXXXXXXX

    2.
    XXXXXXXX
    XXXXXXXX
    XXXXXXXX
    XXXXXXXX

    XXXXXXXXXXXXXXXX
    XXXXXXXX
    XXXXXXXX

    EOD;

    $pdf->writeHTML($tbl, true, false, false, false, ”);

    Thank you very much1

  20. Moses says:

    Hi,
    Please Assist,
    I am trying to output this table but it does not work.?

    Why?

    R
    W
    S
    EOF;
    $i=1;
    while ($i<=3)
    {
    $html .= <<<EOF
    R
    W
    S
    EOF;
    $i++;
    }
    $html .= $html2.<<<EOF

  21. tinhdx says:

    Hi, I use the checkbox as follows: “”. But when printing in pdf checkbox not be “checked”.
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>