<?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>Tom&#039;s Blog &#187; dompdf</title>
	<atom:link href="http://www.encapsulated.org/blog/tag/dompdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.encapsulated.org/blog</link>
	<description>An analog guy in a digital world</description>
	<lastBuildDate>Tue, 15 Dec 2009 20:46:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to let DomPDF and Zend Framework play along</title>
		<link>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/</link>
		<comments>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 18:06:28 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[dompdf]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=77</guid>
		<description><![CDATA[For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was Zend_Pdf. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was <a href="http://framework.zend.com/manual/en/zend.pdf.html">Zend_Pdf</a>. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and every line and text of that PDF. So I went to find another solution: <a href="http://www.digitaljunkies.ca/dompdf/">DomPDF</a>. DomPDF can generates PDF output, from HTML input. Exactly what I wanted and needed. It was however quite tricky to get it working. If you also have problems with this, read on!<br />
<span id="more-77"></span><br />
DomPDF isn&#8217;t part of the Zend Framework library, and as such doesn&#8217;t really follow the ZF conventions. That&#8217;s what caused me trouble: I couldn&#8217;t load the needed classes for it to work.</p>
<p>So how do you set everything up properly?</p>
<p><strong>First step: set include path</strong></p>
<p>When you&#8217;re setting your include paths, just add one for DomPDF.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/../../library/dompdf-0.5.1&quot;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Second step:  make ZF &amp; DomPDF play along</strong></p>
<p>DomPDF has an autoloader which should take care of autoloading the classes for you. All you need to do, is make Zend Framework aware of that autoloader. The solution I found the most was this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">spl_autoload_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However this may not work if you use Zend Framework 1.8 or newer. The real solution I found on a forum, where <a href="http://weierophinney.net/matthew/">Matthew Weier O&#8217;Phinney</a> enlightened another soul with the same problem:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// assuming we're in a controller</span>
<span style="color: #000088;">$autoloader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pushAutoloader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Final step: generate the PDF</strong></p>
<p>That&#8217;s the simplest one, as I found no problems with that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dompdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_paper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a4&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;portrait&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_html</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_base_path</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;document.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There you go, this should set you up to get DomPDF up and running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
