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
//============================================================+
?>

Tags: ,

This entry was posted on Friday, May 9th, 2008 at 9:06 am and is filed under Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

13 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

Leave a Reply