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