<?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>Code-It &#187; python</title>
	<atom:link href="http://lois.di-qual.net/blog/tag/python-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://lois.di-qual.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 30 Oct 2012 19:19:29 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>django-wkhtmltopdf on a linux (Webfaction) server</title>
		<link>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/</link>
		<comments>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/#comments</comments>
		<pubDate>Tue, 04 Sep 2012 13:10:08 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=119</guid>
		<description><![CDATA[Generating PDF with python is not an easy thing. Here is what I&#8217;ve tried: xhtml2pdf: the worst. It has a very bad CSS interpreter and it is pretty impossible to make a clean PDF out from a complex html page. WeasyPrint: that&#8217;s better. It has its own HTML and CSS2 interpreters, and despite some minor [...]]]></description>
				<content:encoded><![CDATA[<p>Generating PDF with python is not an easy thing. Here is what I&#8217;ve tried:</p>
<ul>
<li><a href="http://www.xhtml2pdf.com/">xhtml2pdf</a>: the worst. It has a very bad CSS interpreter and it is pretty impossible to make a clean PDF out from a complex html page.</li>
<li><a href="http://weasyprint.org/">WeasyPrint</a>: that&#8217;s better. It has its own HTML and CSS2 interpreters, and despite some minor issues it is fairly reliable. However it requires <a href="http://lois.di-qual.net/blog/python-install-weasyprint-on-a-dedicated-webfaction-server/">some big dependencies</a>, and I eventually had some memory issues on my webserver.</li>
<li><a href="https://code.google.com/p/wkhtmltopdf/">wkhtmltopdf</a> with its <a href="https://github.com/incuna/django-wkhtmltopdf">Django wrapper</a>: for now, the best. It makes use of Webkit to render HTML+CSS+JS(!) pages, I don&#8217;t think you can do better. However, it requires QT, and this is big. Well, not the &#8220;classical&#8221; QT, a patched QT (that&#8217;s even worse) that allows to run without an X server. This is what I&#8217;m using for now in my projects</li>
</ul>
<p>We&#8217;ll see here how to build wkhtmltopdf and a basic <code class="codecolorer text default"><span class="text">django-wkhtmltopdf</span></code> usage. These steps has been tested on a Webfaction server (CentOS 6) through SSH.</p>
<p><span id="more-119"></span></p>
<h2>The easy way</h2>
<p>Enter this command and go to the &#8220;Post-install&#8221; chapter.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">curl -fsSkL https://raw.github.com/gist/3608048/wkhtmltopdf.sh | sh</div></td></tr></tbody></table></div>
<p>It will install wkhtmltopdf in <code class="codecolorer text default"><span class="text">~/bin</span></code> with its static libraries. </p>
<h2>The hard way &#8211; Let&#8217;s build this</h2>
<p>This is mostly taken from the <a href="https://code.google.com/p/wkhtmltopdf/wiki/compilation">wkhtmltopdf Wiki</a>, but with some no-shared-libraries webserver specifics. Log on SSH, and let the magic begin.</p>
<p>Create a <code class="codecolorer text default"><span class="text">~/download</span></code> folder</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ mkdir -p ~/downloads<br />
$ cd ~/downloads</div></td></tr></tbody></table></div>
<p>Get wkhtmltopdf-qt:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ git clone git://gitorious.org/+wkhtml2pdf/qt/wkhtmltopdf-qt.git wkhtmltopdf-qt<br />
$ cd wkhtmltopdf-qt<br />
$ git checkout staging<br />
$ QTDIR=. ./bin/syncqt</div></td></tr></tbody></table></div>
<p>Configure, and build it! It will take some time (about an hour, which is not much as we disabled a lot of features):</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ ./configure -release -static -fast -exceptions -no-accessibility -no-stl -no-sql-ibase -no-sql-mysql -no-sql-odbc -no-sql-psql -no-sql-sqlite -no-sql-sqlite2 -no-qt3support -xmlpatterns -no-phonon -no-phonon-backend -webkit -no-scripttools -no-mmx -no-3dnow -no-sse -no-sse2 &nbsp;-qt-zlib -qt-libtiff -qt-libpng -qt-libmng -qt-libjpeg -graphicssystem raster -opensource -nomake tools -nomake examples -nomake demos -nomake docs -nomake translations -no-opengl -no-dbus -no-multimedia -openssl -no-declarative -largefile -rpath -no-nis -no-cups -no-iconv -no-pch -no-gtkstyle -no-nas-sound -no-sm -no-xshape -no-xinerama -no-xcursor -no-xfixes -no-xrandr -no-mitshm -no-xinput -no-xkb -no-glib -no-openvg &nbsp;-no-xsync -no-audio-backend -no-sse3 -no-ssse3 -no-sse4.1 -no-sse4.2 -no-avx -no-neon -confirm-license -prefix &quot;../wkqt&quot;<br />
$ gmake -j3<br />
$ gmake install</div></td></tr></tbody></table></div>
<p>Then, download wkhtmltopdf, and build it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cd ~/downloads<br />
$ git clone git://github.com/antialize/wkhtmltopdf.git wkhtmltopdf<br />
$ cd wkhtmltopdf<br />
$ ../wkqt/bin/qmake<br />
$ make</div></td></tr></tbody></table></div>
<p>You now have wkhtmltopdf and its static dependencies in <code class="codecolorer text default"><span class="text">./bin/</span></code>. Let&#8217;s copy them into <code class="codecolorer text default"><span class="text">~/bin</span></code>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ mkdir -p ~/bin<br />
$ cp bin/* ~/bin/</div></td></tr></tbody></table></div>
<h2>Post-installation</h2>
<p>We need <code class="codecolorer text default"><span class="text">~/bin</span></code> in our <code class="codecolorer text default"><span class="text">$PATH</span></code>. That&#8217;s where django-wkhtmltopdf will look for. Add the following line in your <code class="codecolorer text default"><span class="text">~/.bashrc</span></code>.</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">export PATH=$HOME/bin:$PATH<br />
export LD_LIBRARY_PATH=$HOME/lib:$LD_LIBRARY_PATH</div></td></tr></tbody></table></div>
<p>And source it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ source ~/.bashrc</div></td></tr></tbody></table></div>
<p>We&#8217;re done with wkhtmltopdf, let&#8217;s install its Django wrapper.</p>
<h2>django-wkhtmltopdf</h2>
<p>Go to your django app, and install django-wkhtmltopdf with PIP:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ pip install wkhtmltopdf</div></td></tr></tbody></table></div>
<p>Then, in your view:</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> wkhtmltopdf.<span style="color: black;">views</span> <span style="color: #ff7700;font-weight:bold;">import</span> PDFTemplateResponse<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> view<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; PDFTemplateResponse<span style="color: black;">&#40;</span>request<span style="color: #66cc66;">,</span> template_name<span style="color: #66cc66;">,</span> context_dictionary<span style="color: black;">&#41;</span>.<span style="color: black;">rendered_content</span></div></td></tr></tbody></table></div>
<p>Be careful, this last line of code is not documented in the django-wkhtmltopdf&#8217;s wiki. It may be subject to changes in future releases. However, that&#8217;s the only way I found to get a binary-string instead of a HttpResponse.</p>
<p>That&#8217;s all, folks !</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/django-wkhtmltopdf-on-a-linux-webfaction-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python &#8211; Install WeasyPrint on a linux (Webfaction) server</title>
		<link>http://lois.di-qual.net/blog/python-install-weasyprint-on-a-dedicated-webfaction-server/</link>
		<comments>http://lois.di-qual.net/blog/python-install-weasyprint-on-a-dedicated-webfaction-server/#comments</comments>
		<pubDate>Thu, 23 Aug 2012 16:28:39 +0000</pubDate>
		<dc:creator>Loïs Di Qual</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[weasyprint]]></category>
		<category><![CDATA[webfaction]]></category>

		<guid isPermaLink="false">http://lois.di-qual.net/blog/?p=95</guid>
		<description><![CDATA[WeasyPrint is a great python library to generate PDFs from HTML. It beats hands down xhtml2pdf, by interpreting CSS and HTML the right way. However, it has a lot of dependencies and it just can&#8217;t fit in a simple python virtual environment. It took me a whole day to install on Webfaction, so I think [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://weasyprint.org/">WeasyPrint</a> is a great python library to generate PDFs from HTML. It beats hands down <a href="http://www.xhtml2pdf.com/">xhtml2pdf</a>, by interpreting CSS and HTML the right way.</p>
<p>However, it has a lot of dependencies and it just can&#8217;t fit in a simple python virtual environment. It took me a whole day to install on Webfaction, so I think it would be helpful to show how to do it properly.</p>
<p>These steps has been tested on a Webfaction CentOS 6 server (python2.7), and they should be the same for others Linux systems. If your system is not exactly the same, you might have to install other dependencies manually. <a href="http://www.linuxfromscratch.org/">Linux From Scratch</a> is your helpful friend in this kind of situation.</p>
<p><span id="more-95"></span></p>
<p>Log to ssh, and let the magic begin.</p>
<h2>Dependencies</h2>
<p>WeasyPrint relies on the following dependencies:</p>
<ul>
<li>Pango >= 1.29.3 (with introspection)</li>
<li>pycairo</li>
<li>GdkPixbuf >= 2.25 (with introspection, required for images other than PNG)</li>
<li>PyGObject 3.x</li>
</ul>
<p>Of course, these dependencies have their own dependencies. It becomes really hard to install all of them by hand, so I made a small <a href="https://raw.github.com/gist/3438324/weasyprint_webfaction.sh">shell script</a> to do it automatically. It will install all of them in your home directory (with <code class="codecolorer text default"><span class="text">--prefix=$HOME</span></code>).</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ curl -fsSkL https://raw.github.com/gist/3438324/weasyprint_webfaction.sh | sh</div></td></tr></tbody></table></div>
<p>Then, add the following lines to your <code class="codecolorer text default"><span class="text">~/.bashrc</span></code>:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">export LD_LIBRARY_PATH=$HOME/lib<br />
export PYTHONPATH=&quot;$HOME/lib/python2.7/site-packages/:$PYTHONPATH&quot;</div></td></tr></tbody></table></div>
<p>And source it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ source ~/.bashrc</div></td></tr></tbody></table></div>
<h2>Virtual environment</h2>
<p>As we will install global python libraries, we need a virtual environment that is aware of system packages. If you didn&#8217;t create one yet:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cd /path/to/project<br />
$ virtualenv --system-site-packages ./venv</div></td></tr></tbody></table></div>
<p>If your virtual env is already created:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cd /path/to/project<br />
$ mv ./venv/lib/python2.7/no-global-site-packages.txt ./venv/lib/python2.7/_no-global-site-packages.txt</div></td></tr></tbody></table></div>
<h2>WeasyPrint</h2>
<p>Here we are. Install WeasyPrint using the following lines:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ source ./venv/bin/activate<br />
$ pip install weasyprint</div></td></tr></tbody></table></div>
<p>Finally, test it:</p>
<div class="codecolorer-container text twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ weasyprint http://weasyprint.org ./weasyprint-website.pdf</div></td></tr></tbody></table></div>
<p>If there is an import error, please check that you correctly sourced your <code class="codecolorer text default"><span class="text">~/.bashrc</span></code>.</p>
<p>Thanks to <a href="http://community.webfaction.com/users/1146/iliasr/">Ilias Romanos</a> from the Webfaction Q/A forum for the pyCairo part ! Don&#8217;t hesitate to improve the install script on <a href="https://gist.github.com/3438324">github:gist</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lois.di-qual.net/blog/python-install-weasyprint-on-a-dedicated-webfaction-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
