Creating formatted PDFs

Hello,

I’m writing a mail-merge application with Ruby on Rails. I have one
user-created template, wich he creates with a CKEditor (in HTML). I have
a list of members, and in the “mailings” controller I mix the template
with the members data to create a PDF with a letter to every member.
This is all working correctly… Except one part: I don’t know how to
render correctly the HTML in the PDF file.

Currently I use the Prawn library to create the PDF’s. The generation of
the PDF is very easy, but it doesn’t render the HTML codes, so if in the
template appears something like “Hello Mr. {NAME}”, in the final
PDF appears the same text, and not “Hello Mr. {NAME}” in bold…

How can I create PDF’s with Prawn from HTML formatted templates? If it
isn’t possible, which PDF library can I use? I’m using Rails 3.2.

On May 22, 2012, at 11:00 AM, Marc C. wrote:

the PDF is very easy, but it doesn’t render the HTML codes, so if in the
template appears something like “Hello Mr. {NAME}”, in the final
PDF appears the same text, and not “Hello Mr. {NAME}” in bold…

How can I create PDF’s with Prawn from HTML formatted templates? If it
isn’t possible, which PDF library can I use? I’m using Rails 3.2.

I’ve used PrinceXML (not at all free) with great results in the past. It
handles valid and invalid (X)HTML input and generates a very usable PDF
as a result. They have a free license that adds a branded cover page to
the exported PDF, so you can try this out without the investment. They
have great Ruby support and there’s a Rails gem for it, IIRC. (Not
related to them, just an enthusiastic user.)

Walter

Hi,

thought I’d chime in on this issue - as I’ve done a lot of PDF-ing

I took the Flying Saucer (see link below) - and baked in a little
barcode sweetness - but alas it is far from public domain worthy code
(read: no tests, no documentation and spaghetti-code) :frowning:

but, in short - I call

model.html2pdf path_to_template

the html2pdf is a mixin on models that require the print-feature

the mixin renders the path_to_template with ERB, HAML or what-not - to a
tmp-file and then forks a child-process which does a

system “build_pdf %tmpfile pdffile.pdf”

The build_pdf is essentially the flying saucer java getting executed

Don’t know if that helped at all, but it produces nice PDF :slight_smile:

Cheers,
Walther

http://flyingsaucerproject.github.com/flyingsaucer/r8/guide/users-guide-R8.html

Den 22/05/2012 kl. 17.00 skrev Marc C.:

I use PDFKit
https://github.com/pdfkit/PDFKit

2012/5/22 Marc C. [email protected]

the PDF is very easy, but it doesn’t render the HTML codes, so if in the
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Fernando A.
www.fernandoalmeida.net

Marc C. wrote in post #1061690:

Except one part: I don’t know how to
render correctly the HTML in the PDF file.

Currently I use the Prawn library to create the PDF’s.

I use Prawn to generate pdf’s, Nokogiri to parse the html portion and
created an element_to_pdf method to walk the html, looking for tags to
respond to, constructing the pdf.text statements to execute based on the
tag encountered.

Something like:

doc = Nokogiri::HTML(text)
children = doc.children[1].children[0].children

doc / html / body / elements

children.each do |child|
element_to_pdf pdf, child
end

or something like that… code isn’t at hand