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
//============================================================+
?>
14 Responses
Lamyt
06|Jun|2008 1Hi,
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)
yalamber
07|Jun|2008 2can you send me the code or post it in the comment. be sure you are using double quote “” while playing with variables and nor ”.
Lamyt
09|Jun|2008 3It 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
saiful
12|Jun|2008 4Hi 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.
yalamber
12|Jun|2008 5@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.
saiful
13|Jun|2008 6Hi 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.
saiful
13|Jun|2008 7sorry…. one more
if I do $htmlcontent = “$_POST[htmlcontent]“; with out ”, i manage to get the pdf but empty pdf.
yalamber
13|Jun|2008 8Sorry my mistake add following
$htmlcontent=”{$_POST['htmlcontent']}”;
saiful
15|Jun|2008 9thanks yalamber. it works. really milion thanks for your help.
bahrain
20|Jun|2008 10dear yalamber,
i’ve tried this coding but the pdf only contain
$row[\'fetched html content\']
yalamber
20|Jun|2008 11can you send the code you have used to output the pdf. It will help ful to find the error.
bahrain
24|Jun|2008 12already sent an email 2 u..
yalamber
24|Jun|2008 13in which email did you sent. Send in kiratisaathi@gmail.com
Manoj Kumar
04|Oct|2008 14I am unable to config the tcpdf config file, Please guide me.
Leave a reply
Search
Freelance
Tags
buddypress cms community engine freelance kiratisaathi linux modrewrite mundhum pdf personal php phpizabi portfolio scripts SEO session site review socialengine socialnetworking Tutorials ubuntu Uncategorized web designing web development web languages wordpressBlogroll
Recent Posts
Recent Comments
A design creation of Design Disease
Copyright © 2007 - yalamber.com - is proudly powered by WordPress
InSense 1.0 Theme by Design Disease brought to you by HostGator Web Hosting.