<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CoderDan &#187; tcpdf</title>
	<atom:link href="http://www.coderdan.com/tag/tcpdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coderdan.com</link>
	<description>My thoughts on programming, technology and business</description>
	<lastBuildDate>Sat, 26 Jun 2010 16:04:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Create a Pdf in Php Using Tcpdf</title>
		<link>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/</link>
		<comments>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 16:40:30 +0000</pubDate>
		<dc:creator>Daniel Negrea</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tcpdf]]></category>

		<guid isPermaLink="false">http://www.coderdan.com/?p=28</guid>
		<description><![CDATA[There are several ways to generate PDF files in PHP. Each has its strengths and weaknesses. TCPDF strikes a good balance although it is far from ideal.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">jQuery(document).ready(function($) { window.setTimeout('loadFBShareMe_28()',5000); });</script><script type="text/javascript"> function loadFBShareMe_28(){ jQuery(document).ready(function($) { $('.dd-fbshareme-28').remove();$('.DD_FBSHAREME_AJAX_28').attr('width','53');$('.DD_FBSHAREME_AJAX_28').attr('height','69');$('.DD_FBSHAREME_AJAX_28').attr('src','http://widgets.fbshare.me/files/fbshare.php?url=http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/&size=large');  }); }</script><p>There are several ways to generate PDF files in PHP. Each has its strengths and weaknesses. TCPDF strikes a good balance although it is far from ideal.</p>
<p>The best way to transform some web page to php is to let the user print it. With some free software like <a href="http://www.pdfforge.org/">PDFCreator</a>, you can get far better results than anything you can achieve using the available php libraries. Sometimes a client really wants this option or you need to generate some kind of report on the fly. This is when <a href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf">TCPDF</a> is a good choice.</p>
<p>There is no special handling to use the library. You can download it, unpack it and include it in your project. For some advanced features, you might need to allow writing in the <em>cache</em> and <em>images</em> folders. To use it, you include the <strong>tcpdf.php</strong> file and some configuration file. You will need to make a custom configuration for more advanced features but the default one will work in most cases.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you use the library from a cronjob or command line you might get this error: <strong>PHP Notice:  Undefined variable: k_path_url in tcpdf\config\tcpdf_config.php on line 75</strong></p>
<p>This is where a custom configuration file is useful but the fastest way to fix it is to just initialize that variable ahead of the config file. It will work for most of the tasks.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$k_path_url</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The functionality is available using the <strong>TCPDF</strong> class. I think the OOP approach stops here because there are no other classes used (with a minor exception) and I assume the author took this path to compensate for the lack of name spaces in PHP.</p>
<p>The flow is simple: create a TCPDF class, call its methods to generate pages, text and drawings and then save the content. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/config/lang/eng.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tcpdf/tcpdf.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create new PDF document</span>
<span style="color: #000088;">$pdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TCPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// set font</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetFont</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'times'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// add a page</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print a line</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Cell</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Some text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print html formated text</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeHtml</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Html text:&lt;br /&gt;&lt;b&gt;Bold&lt;/b&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// draw a circle</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Circle</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Close and output PDF document</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Output</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'out.pdf'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The library includes a set of examples to demonstrate what it can do.</p>
<p>A TCPDF object maintains internal coordinates used to write text and html with Cell and writeHtml. These methods (and some other related) use these coordinates to maintain the flow of text on the page and add new pages if needed.  They also take into consideration the page margins and headers. The drawing functions are relative to the origin of the page and do not use the Cell coordinates system. This will complicate the job for reports requiring a good layout and a lot of drawing.</p>
<p>The html support is limited. The html needs to be well formatted and only a limited set of tags is supported. There is no error handling for unsupported html so in the best case you will only get some strange php warnings like division by zero and others. You will have to specify the width of the table cells to maintain a minimal control of the layout because the tables are not rendered/positioned as in an usual browser.  I advise against using tables to control the layout of the pdf pages. I assume you read something similar as a web developer.</p>
<p>As a conclusion, I avoid generating complex PDF files in php. TCPDF is the best for the task but it has limited functionality, a poor API and little documentation.</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><script src='http://widgets.digg.com/buttons.js' type='text/javascript'></script><a class='DiggThisButton DiggMedium' href='http://digg.com/submit?url=http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/&amp;title=How+to+Create+a+Pdf+in+Php+Using+Tcpdf'></a></div><div class='dd_button'><script type='text/javascript'>reddit_url = http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/;reddit_title = How+to+Create+a+Pdf+in+Php+Using+Tcpdf;reddit_newwindow='1';</script><script type='text/javascript' src='http://www.reddit.com/static/button/button2.js'></script></div><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/&amp;title=How+to+Create+a+Pdf+in+Php+Using+Tcpdf&amp;t=1' height='68' width='52' frameborder='0' scrolling='no'></iframe></div><div class='dd_button'><script type='text/javascript'>yahooBuzzArticleHeadline=How+to+Create+a+Pdf+in+Php+Using+Tcpdf;yahooBuzzArticleId=http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/;</script><script type='text/javascript' src='http://d.yimg.com/ds/badge2.js' badgetype='square'></script></div><div class='dd_button'><iframe src='http://api.tweetmeme.com/button.js?url=http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/&amp;source=&amp;style=normal' height='61' width='50' frameborder='0' scrolling='no'></iframe></div><div class='dd_button'><a name='fb_share' type='box_count' share_url='http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/' href='http://www.facebook.com/sharer.php'>Share</a><script src='http://static.ak.fbcdn.net/connect.php/js/FB.Share' type='text/javascript'></script></div></div></div><div style='clear:both'></div><!-- Social Buttons Shared Counts Generated by Digg Digg plugin v4.2.2.2, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/ -->]]></content:encoded>
			<wfw:commentRss>http://www.coderdan.com/2009/12/how-to-create-a-pdf-in-php-using-tcpdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
