July 11, 2013

PDF Generation with Plone and PhantomJS

Produce & Publish (Plone Client Connector and Server Component) now supports the generation of PDF using Phantom.js as low-cost (free) alternative to commercial high-quality converters like PrinceXML, PDFreactor or Antennahouse Formatter V6. The quality should be good enough for most cases. The interesting part is that the look & feel of the PDF can be influenced using CSS.

Here is a quick walk-through on using the newest Produce & Publish Plone Client Connector together with the reimplementation of the Produce & Publish Server with Plone 4.3 and Phantom.js

Installation of the Produce & Publish Server

git clone https://bitbucket.org/ajung/pp.server
cd pp.server
python2.7 bootstrap.py
bin/buildout -c complete.cfg
bin/pserve development.ini

The buildout above already installs and configures Phantom.js automatically.

Installation of Plone 4.3 with the decent Produce & Publish Plone Connector

git clone https://bitbucket.org/ajung/pp.client-plone
cd pp.client-plone
python2.7 bootstrap.py
bin/buildout 
bin/instance fg

After starting Plone and creating a new Plone site you can for example create a PDF version of the front-page by adding @@asPDF to the URL of the related object.

Alternatively you can add pp.client-plone to the eggs section of your buildout configuration and re-run buildout afterwards. By default the Plone Client Connector will use PrinceXML (demo version )as converter (which is installed automatically with the pp.server buildout above. You can switch to PhantomJS by setting the environment variable

export PP_CONVERTER=phantomjs

in the shell or through buildout (check the zope2instance recipe documentation).

Example

http://host:port/plone/front-page/@@asPDF

The Produce & Publish Plone Client Connector has basically the same functionality of its older implementation as documented in the Produce & Publish docs.

All this is currently work-in-progress and their might be issues. However the core functionality has been stable for years and will now move a bit to a more modern architecture without throwing all code away. There are several known issues. Most important: all images in Plone are always exported in their original size. Image scaling is either subject to the PDF converter (e.g. by using related width/height on the CSS media=print level) or by applying a scaling transformation as part of the conversion workflows. Please read the code in this case if you want to step forward.

To be clear: all these components are open-source. There is nothing kept back in private. You get what you pay for also applies here regarding the PDF quality. For high-end quality you will need to use converters like PrinceXML or PDFreactor and pay for it. If you don't need features like CMYK support, PDF bookmarks and encryption etc. then you could be very happy with PhantomJS - at least PhantomJS offers a much better quality that you got so far from other tools like Apache-FOP, Reportlab, PISA, htmldoc.

Feedback appreciated, pull requests appreciated. Everything is on Bitbucket. Feel free to contribute...