# Html to pdf in rails

**URL:** <https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847>\
**Category:** Rails\
**Created:** [September 18, 2007, 4:52am UTC](https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847 "2007-09-18T04:52:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [September 18, 2007, 4:52am UTC](https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847/1 "2007-09-18T04:52:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [September 18, 2007, 5:48am UTC](https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847/2 "2007-09-18T05:48:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [September 18, 2007, 9:14am UTC](https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847/3 "2007-09-18T09:14:06Z")

</div>

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://

> **[Prince - Convert HTML to PDF with CSS](https://www.princexml.com/)**
>
> Convert HTML documents to PDF. Beautiful printing with CSS. Support for JavaScript and SVG.

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

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [September 18, 2007, 9:40am UTC](https://www.ruby-forum.com/t/html-to-pdf-in-rails/114847/4 "2007-09-18T09:40:10Z")

</div>

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/](https://xhtmlrenderer.dev.java.net/)

– fxn
