Showing posts with label PDF. Show all posts
Showing posts with label PDF. Show all posts

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

An XML/XHTML/CSS 2.1 Renderer

Release 8 – Final (R8)
April 18, 2009: We are proud to present our R8 release. This release includes no changes since R8 release candidate 2.
Our R8 release includes a number of improvements over R7, as well as bug fixes. You can read the complete list of changes on our news page; here are some
highlights about what you'll find in R8:
  • PDF
    • Upgrade to use iText 2.0.8
    • Support adding custom header properties on PDF output
    • Add ability to set PDF version programmatically
    • Add ability to manipulate PDF output document before it's closed
    • Add ability to have different starting page number for first document in multiple documents
    • Add API to retrieve PDF page and coordinates for boxes with an ID attribute
    • Implement CMYK color support for PDF output
    • Support encryption of PDF output
  • Swing
    • Basic support for selection, highlighting and copying
  • General
    • Expose copy of parsed entities from catalog.
    • Preliminary support for data URLs
    • Support True Type Collection (.ttc) files
    • Preliminary support for Type 1 fonts
    • Support logging via Log4J as an alternative to JDK logging (requires recompile)
    • Handle hidden form elements
    • Rudimentary support for JavaScript links (from Dan Kaplan)
    • Support for callback on form submission
  • CSS
    • Prelimary support for @font-face rules
    • Implement partial support for leader and target-counter (patch from Karl Tauber)
    • Table pagination. When turned on (by setting the -fs-table-pagination property to paginate vs. a default of auto ), tables and table cells will be closed (with appropriate borders and padding) when a page ends and reopened when a page starts. Additionally, a table's thead and tfoot will be repeated on each page.
    • CSS3 margin boxes
    • Named pages
    • Running elements
    • Namespace-aware CSS matching, for example, with attributes [although this also applies to elements]
    • Pseudo-elements may now be specified with a double colon
    • Substring selectors
    • The background property can now be used in a @page context – CSS 3 spec
    • Custom property to limit the scope of the page and pages counters to a portion of the document
    • Custom property that instructs FS to try to avoid breaking a box so that only borders and padding appear on a page

Thursday, June 17, 2010

Wednesday, June 16, 2010

Rethinking PDF Creation in Ruby

What's the big deal?

  1. HTML+CSS - Assuming you're a web developer, there's a good chance that you already know HTML and can work with it efficiently.
  2. CSS3 - We get WebKit's CSS3 support for free. This means effects like drop shadows, rounded borders, transformations and others are super-easy. (Note: effects requiring blur radius do not work.)
  3. Testing - We have tools built into our normal workflow for testing HTML. You can even use Cucumber to drive the development of a PDF with PDFKit.
To give you an idea of how well this fits into our normal workflow here at Relevance, this is how we built out our PDF reports:
  1. Our designer mocked up a sample PDF and converted it to HTML+CSS.
  2. Using Cucumber to drive development, we created a controller action to generate this HTML view of the PDF. (It was just another URL in our app.)
  3. We added a screen-only stylesheet to the HTML that mimics the look of a PDF reader. This allowed us to get a feel of how it would look as a PDF.
  4. Using a bit of Rack Middleware that ships with PDFKit, we can get the PDF version of that web page by simply appending '.pdf' to the url.
  5. We're done. No crazy extra class to handle PDF rendering. No need to spend all day reading through docs to learn the obscure code and magic incantations required to generate your PDF.

Samples

  • PDF of google.com - PDF rendered from http://google.com
  • CSS3 Examples - Sample rendering of common CSS3 effects including border-radius, text-shadow, box-shadow, and border-image. Notice the lack of a blur radius on text-shadow and box-shadow.
  • Sample HTML page with PDF viewer CSS - Example of using a single HTML source to render both a screen version and a PDF version. Uses a media="screen" and media="all" to mark relevant CSS.
  • PDF generated from PDF viewer HTML - PDF generated from sample HTML above. You must tell PDFKit to only use print stylesheets in order to achieve this effect (PDFKit.new(html, :print_media_type => true)).
http://thinkrelevance.com/blog/2010/06/15/rethinking-pdf-creation-in-ruby.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+relevance-llc+%28Relevance,+Inc.%29&utm_content=Google+Reader

Monday, April 27, 2009

jsPDF

jsPDF is an open-source library for generating PDF documents using nothing but Javascript. You can use it in a Firefox extension, in Server Side Javascript and with Data URIs in some browsers.

http://code.google.com/p/jspdf
http://ajaxian.com/archives/dynamically-generic-pdfs-with-javascript

Monday, January 19, 2009

HTML / CSS to PDF using Ruby on Rails

Ever tried to save a web page and send it to someone? What about printing a web page? Both pretty much suck.

A lot of people are talking about printing with HTML and CSS these days but what they don’t tell you is the following:

With CSS you can’t…
  • Determine where page breaks happen.
  • Set page size or type (landscape or portrait).
  • Print background colors…some browsers don’t even print images.
  • Set page footers.

Wow, that’s lame.

Ever tried sending someone a web page? Not a link, but the actual web page. It doesn’t work well at all.

http://sublog.subimage.com/2007/05/29/html-css-to-pdf-using-ruby-on-rails