Showing posts with label HTML2PDF. Show all posts
Showing posts with label HTML2PDF. Show all posts

Monday, November 14, 2011

WeasyPrint

WeasyPrint is a visual rendering engine for HTML and CSS that can export to PDF. It aims to support web standards for printing. WeasyPrint is free software released under the AGPL license.
It is based on libraries for parsing, text and drawing but not on full rendering engines like WebKit on Gecko. The CSS visual rendering is written in Python and meant to be easy to hack on.
Get started by installing it or jump to:

Sample output

As an example, here is the introduction chapter of the CSS 2.1 spec rendered with WeasyPrint: CSS21-intro.pdf. It was obtained by running:
weasyprint http://www.w3.org/TR/CSS21/intro.html CSS21-intro.pdf

Current status

WeasyPrint 0.1 can fetch a remote web page from an URL, find and fetch associated stylesheets and images, and render it all to PDF or PNG.
Floats, absolute positioning and tables are not supported yet, but WeasyPrint can already be useful for pages with “simple” layout. See the features page for what exactly is supported or not.
Tables (among other things) are being worked on and should be in the next version.

http://weasyprint.org/

Friday, July 16, 2010

HTML to PDF Conversion


Once a business web application reaches a certain size, the need often arises to generate PDFs from HTML/CSS.
Up until recently, the story around this for a MRI Rails application was not good. You could either use tools like Prawn, which require a description of the layout in a specific DSL, or pay for a tool like Prince XML which can convert from HTML, but which costs quite a bit. Those using JRuby were in a stronger position as they could use the Java PDF library called Flying Saucer.
The good news is that PDF generation for MRI Ruby is now easy and free, thanks towebkit, the open source webkit wrapper called wkhtmltopdf and mileszs's wickedpdf plugin. I was really excited to come across this plugin and started to use it right away. However, it had a couple of issues:
  • Temp file handling caused errors when two PDFs were being generated within the same second (eg, 2 requests at almost the same time)
  • Problems generating PDF were not reported
Galdomedia forked the code and updated it to use standard Ruby temp files. This was great for ruby 1.7, but not good for Ruby 1.6 which does not allow you to set the extension on temp files (wkhtmltopdf relies on having a .html extension).
As my production servers run Ruby 1.6, I needed a different approach. My fork uses streams rather than temporary files, and adds some basic error handling and basic integration tests.
To install in a rails app:
script/plugin install git://github.com/jcrisp/wicked_pdf.git