<?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>KennyNet &#187; pdf</title>
	<atom:link href="http://www.kennynet.co.uk/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kennynet.co.uk</link>
	<description>Ramblings of a PHP developer</description>
	<lastBuildDate>Fri, 02 Dec 2011 14:05:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP and PDF Templates</title>
		<link>http://www.kennynet.co.uk/2008/11/26/php-and-pdf-templates/</link>
		<comments>http://www.kennynet.co.uk/2008/11/26/php-and-pdf-templates/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 21:30:13 +0000</pubDate>
		<dc:creator>Kenny Millington</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[pdf templates]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.kennynet.co.uk/?p=27</guid>
		<description><![CDATA[Recently I had to write a class that could generate PDF payslips. Going for the easy option I wanted to take the existing template to re-use it and overlay the text from the database onto the template rather than redraw the entire payslip layout (which is just simply time consuming in PHP). My chosen PDF [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to write a class that could generate PDF payslips. Going for the easy option I wanted to take the existing template to re-use it and overlay the text from the database onto the template rather than redraw the entire payslip layout (which is just simply time consuming in PHP).</p>
<p>My chosen PDF library is <a title="FPDF" href="http://www.FPDF.org">FPDF</a> so my first attempt was to create a graphic (png/jpeg) of the existing template and use the Image() function in FPDF to add the image to the PDF. However, this didn&#8217;t work as well as I&#8217;d have hoped since the image became very blurry. I believe this is due to the image being output at 72dpi onto the PDF when it actually needs greater resolution to be usable.</p>
<p>Obviously a better solution was needed, enter <a title="FPDI" href="http://www.setasign.de/products/pdf-php-solutions/fpdi/">FPDI</a> which allows you to load a PDF file as a template. So now all I needed to do was use the payslip PDF as a template and just overlay the text onto the template. FPDI makes this extremely simple &#8211; just include the required class file and do the following:-</p>
<pre>$pdf = new FPDI();
$count = $pdf-&gt;setSourceFile("payslip-template.pdf");
$template = $pdf-&gt;import(1, "/MediaBox");
$pdf-&gt;addPage();
$pdf-&gt;useTemplate($template);</pre>
<p>Now all you have to do is populate your content into/over the template and output it as normal.</p>
<p>One caveat I did notice was that FPDI seemed to be sending the files out with the mime type &#8220;application/x-download&#8221; rather than &#8220;application/pdf&#8221; which caused my browser to not know what to do with the file if I chose to open it rather than save it. My solution was to reset the Content-type header after outputting the PDF by taking advantage of existing output buffering in my application:-</p>
<pre>ob_clean();
$pdf-&gt;Output("payslip.pdf", "D");
header("Content-type: application/pdf");
exit;</pre>
<p>This works because <a href="http://uk2.php.net/outcontrol">output buffering</a> allows us to change the headers that have already been output. If you do not currently have output buffering enabled in your code change the &#8220;ob_clean();&#8221; line to &#8220;ob_start();&#8221;.</p>
<p>FPDI is released under the <a href="http://www.apache.org/licenses/LICENSE-2.0.html">Apache Software License, Version 2.0</a> which is <a href="http://www.apache.org/licenses/GPL-compatibility.html">compatible</a> with the <a href="http://www.gnu.org/licenses/gpl-3.0.html">GNU GPL Version 3</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kennynet.co.uk/2008/11/26/php-and-pdf-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

