# PDF::Writer / railspdf - building a layout?

**URL:** <https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671>\
**Category:** Rails\
**Created:** [June 5, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671 "2008-06-05T22:40:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 5, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/1 "2008-06-05T22:40:29Z")

</div>

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:

/app/views/layouts/foo.rpdf:

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?

Thanks,

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 8:45am UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/2 "2008-06-06T08:45:58Z")

</div>

I believe there is a different plugin available that lets you use  
standard layouts/html views, but they are rendered as pdf’s, checkout  
[http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs](http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs)

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/3 "2008-06-06T12:08:15Z")

</div>

On 06 Jun 2008, at 05:05, Jon Pospischil wrote:

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

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](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

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/4 "2008-06-06T16:09:35Z")

</div>

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

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,  
apparently.

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?

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/5 "2008-06-06T16:12:32Z")

</div>

Jon,

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)?

Thanks!

-Danimal

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/6 "2008-06-06T22:34:36Z")

</div>

Can’t say as I’ve done it just the way you’re looking for…

Some might want to burn me at the stake, but I use pdf-writer to  
generate my reports by having the controllers instantiate and setup the  
pdf (a very gross approximation of your template?), then “tell” the  
model to render itself on the pdf in a certain mode (‘full’ or ‘short’  
or ‘testing’, ‘imaged’ or ‘noimage’).

Actual pdf writing code goes in each model, which knows how to render  
itself in a given mode (internally a model checks the mode, then uses  
its method for that mode if supported… that would be the extent of my  
“dryness”).

If the report is for a “full” pdf for example, the model will pass the  
pdf it rendered itself on to each of its children in order, invoking  
their render\_on\_pdf method for the same mode before returning the pdf to  
the requester (be it a controller or another model, it doesn’t care).

This hierarchy gives me a nice front to back rendering of Projects,  
Scenarios, Tests, and Screenshots that can be started at any point in  
the hierarchy… The controller eventually sends the pdf back downstream  
to the user via send\_data.

When I have the time, I’m going to look into the respond\_to route, but  
there are alot of other things in the queue ahead of replacing something  
that works for now.

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 6, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/7 "2008-06-06T16:43:14Z")

</div>

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  
railspdf)  
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.

---

<div class="post-metadata">

**Author:** ![Danimal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Danimal](https://www.ruby-forum.com/u/Danimal)\
**Post date:** [June 10, 2008, 5:03am UTC](https://www.ruby-forum.com/t/pdf-writer-railspdf-building-a-layout/139671/8 "2008-06-10T05:03:59Z")

</div>

Ar,

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

-Danimal
