Html to pdf in rails

I’ve been searching for a way to easily convert an html page into a pdf
in my rails app and I haven’t found anything worth calling home about.
Has anyone found something good out there?

David M. wrote:

I’ve been searching for a way to easily convert an html page into a pdf
in my rails app and I haven’t found anything worth calling home about.
Has anyone found something good out there?

There is no easy way to to this in any language actually. They all have
ways to parse the HTML into an XML And then apply a style sheet or
something on top of it to converter and create your PDF, but it’s just
not easy.

(Well if you have a good printer driver that prints pdf… problem solved
but that’s another issue)

Okay so here’s what I have found out from my experiences on this EXACT
topic, I would LOVE to stand corrected if my info is older and outdated
and an easy solutions is around somewhere.

Part of the difficulty in finding PURE (I.E. API / Classes) coding
solution to do this in Ruby, Perl etc is is due to difficulty in dealing
with the the incompatibilities of the HTML / XHTML and how people
implement it. Some HTML ‘looks’ well formed in the browser but in
reality the document wont even pass a validation check through a schema
/ dtd / xsd because of bad or missing tags.

Then you have the issues with style sheets and display tags in the
markup. Which one take precedence… lastly what if this page has non
html display elements like flash or something like that.

As you can see ball of hell waiting to happen. At the company I work for
I, and my team, have been tasked to look at a non-proprietary way of
doing this for months. All the 'easy 'solutions require a printer like
driver or engine to be running on the machine handling the request. Once
you have it all parsed say in a broswer then have THAT converted for you
is much easer than writing your own parser. But, not really what you are
looking for… so I’ll continue…

To actually try ANSWER your question, you will have to use a combination
of PDF::Writer, one of the XML tools and a way to convert your CSS and
styles into PDF tags. I would use XML just to make the iteration of the
DOM easy to convert element by element into PDF. The XML tools I know of
are libXML-Ruby and/or the whole XML Line of classes in the Ruby CORE
Api.

Hope this at LEAST gets you started in the right direction.

On 18 Sep 2007, at 04:52, David M. wrote:

I’ve been searching for a way to easily convert an html page into a
pdf
in my rails app and I haven’t found anything worth calling home about.
Has anyone found something good out there?

If you have a nice big budget, there’s PrinceXML: http://

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

Best regards

Peter De Berdt

On Sep 18, 2007, at 4:52 AM, David M. wrote:

I’ve been searching for a way to easily convert an html page into a
pdf
in my rails app and I haven’t found anything worth calling home about.
Has anyone found something good out there?

If shelling out is an option (or perhaps JRuby, but I have not tried
it with this library) you could have a look at Flying Saucer:

https://xhtmlrenderer.dev.java.net/

– fxn