<?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>Screenage &#187; Php</title>
	<atom:link href="http://www.screenage.de/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.screenage.de/blog</link>
	<description></description>
	<lastBuildDate>Sun, 13 Nov 2011 10:33:53 +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>my package of the day &#8211; htmldoc &#8211; for converting html to pdf on the fly</title>
		<link>http://www.screenage.de/blog/2008/07/07/my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly</link>
		<comments>http://www.screenage.de/blog/2008/07/07/my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 06:15:31 +0000</pubDate>
		<dc:creator>ccm</dc:creator>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[CommandLine]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[PackeOfTheDay]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.screenage.de/blog/2008/07/07/my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly/</guid>
		<description><![CDATA[PDF creation got actually fairly easy. OpenOffice.org, the Cups printing system, KDE provide methods for easily printing nearly everything to a PDF file right away. A feature that even outperforms most Windows setups today. But there are still PDF related &#8230; <a href="http://www.screenage.de/blog/2008/07/07/my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>PDF creation got actually fairly easy. OpenOffice.org, the Cups printing system, KDE provide methods for easily printing nearly everything to a PDF file right away. A feature that even outperforms most Windows setups today. But there are still PDF related task that are not that simple. One I often run into is automated PDF creation on a web server. Let&#8217;s say you write a web application and want to create PDF invoices on the fly.</p>
<p>There are, of course, PDF frameworks available. Let&#8217;s take PHP as an example: If you want to create a PDF from a php script, you can choose between <a href="http://www.fpdf.org/">FPDF</a>, <a href="http://www.digitaljunkies.ca/dompdf/">Dompdf</a>, the sophisticated <a href="http://framework.zend.com/">Zend Framework</a> and more (and commercial solutions). But to be honest, they are all either complicated (as you often have to use a specific syntax) to use or just quite limited in their possibilities to create a pdf file (as you can only use few design features). As I needed a simple solution for creating a 50+ pages pdf file with a huge table on the fly I tested most frameworks and failed with most of them (often just as I did not have enough time to write dozens of line of code).</p>
<p>So I hoped to find a solution that allowed me just to convert a simple HTML file to a PDF file on the fly providing better compatibility than Dompdf for instance. The solution was &#8230; uncommon. It was no PHP class but a neat command line tool called &#8220;<a href="http://www.htmldoc.org/">htmldoc</a>&#8221; available as a package. If you want to give it a try just install it by calling &#8220;aptitude install htmldoc&#8221;.</p>
<p>You can test htmldoc by saving some html files to disk and call &#8220;htmldoc &#8211;webpage filename.html&#8221;. There a lot of interesting features like setting font size, font type, the footer, color and greyscale mode and so on. But let&#8217;s use htmldoc from PHP right away. The following very simple script uses the PHP output buffer for minimizing the need for a write to disk to one file only (if somebody knows a way of using this without any temporary files from a script, let me know):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">// start output buffer for pdf capture
&nbsp;
ob_start();
?&gt;
your normal html output will be places here either by
dumping html directly or by using normal php code
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// save output buffer</span>
<span style="color: #000088;">$html</span><span style="color: #339933;">=</span><span style="color: #990000;">ob_get_contents</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;">// delete Output-Buffer</span>
<span style="color: #990000;">ob_end_clean</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;">// write the html to a file</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./tmp.html'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Could not open <span style="color: #006699; font-weight: bold;">$filename</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Could not write <span style="color: #006699; font-weight: bold;">$filename</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// htmldoc call</span>
<span style="color: #000088;">$passthru</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'htmldoc --quiet --gray --textfont helvetica \
--bodyfont helvetica --logoimage banner.png --headfootsize 10 \
--footer D/l --fontsize 9 --size 297x210mm -t pdf14 \
--webpage '</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// write output of htmldoc to clean output buffer</span>
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">passthru</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$passthru</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pdf</span><span style="color: #339933;">=</span><span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ob_end_clean</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;">// deliver pdf file as download</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: application/pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: attachment; filename=test.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-length: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">strlen</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: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$pdf</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, this is neither rocket science nor magic. Just a wrapper for htmldoc enabling you to forget about the pdf when writing the actual content of the html file. You&#8217;ll have to check how htmldoc handles your html code. You should make it as simple as possible, forget about advanced css or nested tables. But it&#8217;s actually enough for a really neat pdf file and it&#8217;s fast: The creating of 50+ page pdf files is fast enough in my case to make the on demand access of htmldoc feel like static file usage.</p>
<p>Please note: Calling external programs and command line tools from a web script is always a security issue and you should carefully check input and updates for the program you are using. The code provided should be easily ported to another web language/framework like Perl and Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.screenage.de/blog/2008/07/07/my-package-of-the-day-htmldoc-for-converting-html-to-pdf-on-the-fly/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>fast installation of apc php optimizer/cache on Debian / Ubuntu</title>
		<link>http://www.screenage.de/blog/2008/02/14/fast-installation-of-apc-php-optimizercache-on-debian-ubuntu/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fast-installation-of-apc-php-optimizercache-on-debian-ubuntu</link>
		<comments>http://www.screenage.de/blog/2008/02/14/fast-installation-of-apc-php-optimizercache-on-debian-ubuntu/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 12:01:31 +0000</pubDate>
		<dc:creator>ccm</dc:creator>
				<category><![CDATA[Debian]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.screenage.de/blog/2008/02/14/fast-installation-of-apc-php-optimizercache-on-debian-ubuntu/</guid>
		<description><![CDATA[If you want a fast installation of the php apc bytecache/optimizer for PHP5/Apache2, try the following snippet when already running a standard PHP5/Apache2 environment: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # install &#8230; <a href="http://www.screenage.de/blog/2008/02/14/fast-installation-of-apc-php-optimizercache-on-debian-ubuntu/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you want a fast installation of the <a href="http://pecl.php.net/package/APC" title="APC">php apc bytecache/optimizer</a> for PHP5/Apache2, try the following snippet when already running a standard PHP5/Apache2 environment:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># install dependencies for compilation</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> apache2-dev php5-dev build-essential
<span style="color: #666666; font-style: italic;"># get current version of apc - check if there is a newer one!</span>
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>pecl.php.net<span style="color: #000000; font-weight: bold;">/</span>get<span style="color: #000000; font-weight: bold;">/</span>APC-3.0.16.tgz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xzf  APC-3.0.16.tgz
$ <span style="color: #7a0874; font-weight: bold;">cd</span>  APC-3.0.16
$ phpize
$ .<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-apc</span> <span style="color: #660033;">--enable-apc-mmap</span> \
  <span style="color: #660033;">--with-apxs</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>apxs2 \
  <span style="color: #660033;">--with-php-config</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>php-config
$ <span style="color: #c20cb9; font-weight: bold;">make</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #666666; font-style: italic;"># in /etc/php5/apache2/php.ini add: extension=apc.so</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> apache2ctl restart</pre></td></tr></table></div>

<p>No a phpinfo(); should show you a new apc section.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.screenage.de/blog/2008/02/14/fast-installation-of-apc-php-optimizercache-on-debian-ubuntu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

