PDF::Writer / railspdf - building a layout?

I'm using railspdf plugin and the pdf-writer gem. It's working really
well, but I wanted to see if it were possible to create a layout
template, much like normal HTML.

I tried just creating a layout file in the layouts directory but I
can't include embedded ruby (erb) code. I.e. I had:


  pdf.select_font "Times-Roman"
  pdf.text "WELCOME", :font_size => 72, :justification => :center
  <%= yield %>

But that didn't work. If I took out the <%= yield %> it showed the
"WELCOME" but not the content from the rendered file.

Has anyone else gotten PDF generation to work with pdf-writer (and
optionally railspdf) but using a template layout as well as an action-
rendered view?



I believe there is a different plugin available that lets you use
standard layouts/html views, but they are rendered as pdf's, checkout

That can be done with PrinceXML and the procedure can be found at http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails

Do know that PrinceXML is commercial software and has a very hefty price tag if you’re only looking into making a few simple PDFs. That said, the results are astonishing and their support is great.

Best regards

Peter De Berdt

Hi Peter!

Thanks for the reference to PrinceXML. I think the price-tag alone
will rule it out, but I'll take a look.

The thing I like about railspdf and pdf-writer is that it is very
simple to use and pdf-writer is fairly mature so there are lots of
controls. It's just that it's not very dry, for me, because I can't
figure out how to use partials or templates with it. So each PDF I
generate has a rpdf view and that view contains all the code for that
PDF... and so my PDF views have a lot of redunancy between them.

For example, in my app, I want to generate PDFs for job postings,
either individually or for a list. Well, the DRY way to do that would

1) have a display template that creates header, footer, and styles
2) have a partial to show the content for a single job posting
3) for a list of job postings, iterate and call the partial.

For HTML, this is easy, normal rails, stuff. But for the PDFrails/pdf-
writer, I'm not sure how to do this (or if I even can). I can't figure
out how to call partials from within the .rpdf file. And it doesn't do
eruby so I can't do loops/iterations. All I have are variables,

So maybe I need to look for something else, but I'm really hopeful
that I'm just missing some simple stuff with pdfrails and/or pdf-
writer. Anyone out there that can help with this?



Yeah, I've seen that page. Problem is, it's just a list, effectively.
I suppose I could wade through them and try them out, but I would love
it if someone else who has already done that could post their
comparison findings. I.e. "use this or this for these needs, use that
or that for these others". For example, PrinceXML seems very powerful,
but expensive, so it's probably not the right fit for a small, non-
commercial project... or tight-budget project. Also, I've used early
versions of the HTMLDOC wrapper in the past, but it was kinda
complicated to set it up.

Anyway, Jon, what plugin were you specifying in your post? (that lets
you use layouts/views as normal)?



I've used pdf-writer and railspdf only for some very limited cases,
just making some invoices for customers, so I can't say very much
about it and may be wrong.
By now I didn't come about anything that would say, that either of
them runs anything through erb.(which would be a nice feature for
But of course there is always the option to use erb manually, and feed
the result into railspdf.
I haven't done this myself and so i'm not sure how difficult that is.
I remember that there where some issues with binding your projects
local variables if you don't use the render command. Maybe even
render :file => "foo.rpdf"
would return something useful? After all erb doesn't care of anything
outside <%...%>

As you see, I can only throw in some ideas and you may have to work
out the details if you find that worth trying.


Very interesting... turns out that before I read this, I ended up
doing it this way. All the model-specific rendering is done on the
model. I also created a pdf_helper file that does a header and footer
and some other helper-ish things.

The nice thing is that if you add railspdf to the mix, you get the
respond_to. That's effectively what railspdf does (it's not a lot of
code so it's easy to figure out what it's doing). And it makes the
controller REALLY clean.

The only thing that I would prefer would be if I could do render
partials or yields in the .rpdf template, but it looks like that is
limited by how railspdf is written. Ahh well.

At least it's working and it's reasonably clean, although layout is
handled by models + helpers instead of: views + partials