<?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>Wanderwort by Roman Ernst &#187; PHP</title>
	<atom:link href="http://wanderwort.de/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://wanderwort.de</link>
	<description>Interkulturelle Wortkolletionen in Ruby, PHP, XHTML, CSS nah am Strom der Gesellschaft</description>
	<lastBuildDate>Mon, 14 Mar 2011 15:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>HTML to PDF Library for PHP 5</title>
		<link>http://wanderwort.de/2009/07/08/html-to-pdf-library-for-php-5/</link>
		<comments>http://wanderwort.de/2009/07/08/html-to-pdf-library-for-php-5/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 19:06:14 +0000</pubDate>
		<dc:creator>pex</dc:creator>
				<category><![CDATA[Gemachtes]]></category>
		<category><![CDATA[Arbeit]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://wanderwort.de/?p=138</guid>
		<description><![CDATA[As I had to build a simple html to pdf converting for a client using php, I found a beautiful library called DOMPDF written by Benj Carson. It seemed to be very lightweight and uncomplicated, which turned out to be true. In a standard case it really is as simple as $pdf-&#62;load_html&#40;$html&#41;; I am going [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wanderwort.de/2009/07/08/html-to-pdf-library-for-php-5/"><img src="http://wanderwort.de/wp-content/uploads/2009/07/dompdf_simple.png" alt="DomPDF HTML to PDF" title="dompdf_simple" width="200" height="76" class="size-full wp-image-146" style="float: left; margin: 0 15px 5px 0" /></a> As I had to build a simple html to pdf converting for a client using php, I found a <a href="http://www.digitaljunkies.ca/dompdf/">beautiful library called DOMPDF</a> written by Benj Carson.</p>
<p>It seemed to be very lightweight and uncomplicated, which turned out to be true. In a standard case it really is as simple as</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pdf</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></pre></div></div>

<p>I am going to talk about the basic usage and problems I had to solve, specially with umlauts / non latin characters.<br />
<span id="more-138"></span></p>
<h3>Basic Usage</h3>
<p>To initiate an environment you just create a new instance of the given class.</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;">'#path-to-your-installation#/dompdf/dompdf_config.inc.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_html</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#some-html#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdf</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;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'some-file.pdf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Further information about the basic usage of the class can be found on the <a href="http://www.digitaljunkies.ca/dompdf/usage.php">project page</a>. I am only using it inline PHP.</p>
<h3>Convert your CSS styled HTML</h3>
<p>Now you have to feed the method load_html with some html. Therefore we build a whole, simple page including as stylesheet (css). One of the most important things about this class is, that you simply link to a css file and dompdf parses and uses it to style your pdf.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;some-css.css&quot;</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Your Content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Please notice that dompdf neither supports absolute positioning nor floating. Therefor we have to use backgound-image-position to position images. Remember not to use the short-form &#8220;background&#8221; as this isn&#8217;t supported neither.</p>
<h3>Fixing White-Spaces</h3>
<p>I had the problem of wrong white-spaces respectively line-breaks because I had to convert a german document. tommylacroix found <a href="http://www.dashinteractive.net/dompdf/index.php?v=3153736">a solution</a> that worked perfectly for me.<br />
You have to go into your text_frame_decorator.cls.php around line 127 and replace</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> split_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$split</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_frame<span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_node</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">splitText</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$deco</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$split</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>by</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> split_text<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$offset</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
      <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_frame<span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_node</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$split</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_frame<span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_node</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">splitText</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_frame<span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_node</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$split</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$deco</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$split</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Using inline PHP embedded in HTML</h3>
<p>Last but not least there are several more options to manipulate the rendered pdf file. By adding some inline php code in your html we can manage to display page numbers, a header or footer, watermarks, etc.</p>
<p>To add page numbers to your pdf, we add the following code to our html:<br/> (I explained the usage in comments)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/php&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">## Setup a tiny configuration ##
</span>      <span style="color: #000088;">$text_height</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">17</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$text_size</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$font</span> <span style="color: #339933;">=</span> Font_Metrics<span style="color: #339933;">::</span><span style="color: #004000;">get_font</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;helvetica&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;normal&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">## Get the dimensions of our pdf-file ##
</span>      <span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;width&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;height&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">## The Counter-Text with variables: ##
</span>      <span style="color: #000088;">$pageCounterText</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Page: {PAGE_NUM} of {PAGE_COUNT}&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">## If you want to position the Counter on the right, you need the text-width: ##
</span>      <span style="color: #000088;">$pageCounterWidth</span> <span style="color: #339933;">=</span> Font_Metrics<span style="color: #339933;">::</span><span style="color: #004000;">get_text_width</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageCounterText</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">## Not we can add this to our pdf:
</span>      <span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">page_text</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;height&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #000088;">$text_height</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pageCounterText</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text_size</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</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: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></div></div>

<p>The page_text method works like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  page_text<span style="color: #009900;">&#40;</span>float <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> float <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> float <span style="color: #000088;">$size</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</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: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">## Explanation: ##
</span>    <span style="color: #000088;">$x</span><span style="color: #339933;">:</span> Position from the top
    <span style="color: #000088;">$y</span><span style="color: #339933;">:</span> Position from the left
    <span style="color: #000088;">$size</span><span style="color: #339933;">:</span> Font<span style="color: #339933;">-</span>Size
    <span style="color: #000088;">$color</span><span style="color: #339933;">:</span> an rgb<span style="color: #339933;">-</span><span style="color: #990000;">array</span> <span style="color: #339933;">-&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>R<span style="color: #339933;">,</span> G<span style="color: #339933;">,</span> B<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>optional<span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wanderwort.de/2009/07/08/html-to-pdf-library-for-php-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

