# Rails view as PDF... suggestions?

**URL:** <https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124>\
**Category:** Ruby\
**Created:** [November 16, 2006, 2:40am UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124 "2006-11-16T02:40:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 16, 2006, 2:40am UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/1 "2006-11-16T02:40:25Z")

</div>

Hi, everybody!

I am about to do some research tomorrow, but if any of you has an  
obvious answer, then that would help. I am looking to build PDFs from  
Rails views, and I’d like some guidance on how to go about it.

If this is not the right forum, feel free to forward me to a better one.

I want to put placeholders, both text and images, onto a template and  
end up with a PDF. I’m pretty open about how that all happens, so feel  
free to make wild suggestions. The only important constraint I can think  
of is that layout (meaning spacing) matters, so relying on a browser  
rendering HTML is probably not an option. The template will be a kind of  
background on which the placeholders will be laid out, and I imagine  
it’s the template that will be the tricky part.

I have looked into PDF::Writer, but I’m not sure whether we’ll need to  
draw the templates or not. If we get the template as a pre-rendered file  
(say PDF, RTF, whatever), then I’d like to be able to overlay the  
placeholders onto the template and merge.

So, any suggestions? Any questions for me? Any choice Google keywords  
you care to suggest? I could really use your help.

Take care.

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 16, 2006, 3:30am UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/2 "2006-11-16T03:30:30Z")

</div>

Hi,

2006/11/16, J. B. Rainsberger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am about to do some research tomorrow, but if any of you has an  
> obvious answer, then that would help. I am looking to build PDFs from  
> Rails views, and I’d like some guidance on how to go about it.

> I want to put placeholders, both text and images, onto a template and  
> end up with a PDF. I’m pretty open about how that all happens, so feel  
> free to make wild suggestions. The only important constraint I can think  
> of is that layout (meaning spacing) matters, so relying on a browser  
> rendering HTML is probably not an option. The template will be a kind of  
> background on which the placeholders will be laid out, and I imagine  
> it’s the template that will be the tricky part.

My suggestion is the following:

1. make a SVG for template with Illustrator, Inkscape and so on
2. substitute placeholders in the SVG by ERB, Amrita and so on
3. generate PDF from the substituted SVG.  
(See [ruby-talk:225127])

# You can’t use PDF::Writer for this suggestion.

Thanks,

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 16, 2006, 7:45pm UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/3 "2006-11-16T19:45:07Z")

</div>

PDF::Writer – it kicks ass and it’s easy to use.

[http://www.artima.com/rubycs/articles/pdf\_writer.html](http://www.artima.com/rubycs/articles/pdf_writer.html)

require “pdf/writer”

```
  class HelloController < ApplicationController
    # ...
    def pdf
      _pdf = PDF::Writer.new
      _pdf.select_font "Times-Roman"
      _pdf.text "Hello, Ruby.", :font_size => 72, :justification => 

```

:center

```
      send_data _pdf.render, :filename => "hello.pdf",
                :type => "application/pdf"
    end
    # ...
  end

```

the only problem with it is that you can’t define templates and then  
have PDF::Writer process them the same way you can process other  
templates within Rails as views.

however, it sounds as if that’s what your application needs, and I  
think creating a templating facility for PDF::Writer would be your  
best bet. if you create it, please open-source it! it’ll make a useful  
library stunningly useful.

(actually, I might be willing to help with that, I think it would be  
very, very cool.)

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 16, 2006, 2:52am UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/4 "2006-11-16T02:52:27Z")

</div>

On Thu, 16 Nov 2006, J. B. Rainsberger wrote:

> wild suggestions. The only important constraint I can think of is that layout  
> So, any suggestions? Any questions for me? Any choice Google keywords you  
> care to suggest? I could really use your help.
> 
> Take care.

look at this too

[http://www.kitp.ucsb.edu/~paxton/tioga.html](http://www.kitp.ucsb.edu/~paxton/tioga.html)

depending on your needs it might fill them quite nicely

pdf::writer is nice too - i think they are taking quite different  
approaches  
and fill different niches.

-a

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 16, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/5 "2006-11-16T21:03:26Z")

</div>

On 11/15/06, Kouhei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end up with a PDF. I’m pretty open about how that all happens, so feel
> 
> 1. generate PDF from the substituted SVG.  
> (See [ruby-talk:225127])

You can also use MasterView to generate xml (for SVG or to use with  
XSL-FO).  
We don’t have any special directives for this purpose but many of the  
standard ones would work nicely.

In the java world we would use xsl-fo and an FO processor like Apache  
FOP to  
generate PDF’s. A low tech way to do this would be to have ruby shell  
out  
and launch FOP to convert the XSL-FO formatted xml to PDF.

I don’t know if there are other C/C++ libraries (commercial or  
otherwise)  
that could do the same thing.

Jeff B.  
MasterView Template engine project founder  
[http://masterview.org/](http://masterview.org/)  
[http://rubyforge.org/projects/masterview](http://rubyforge.org/projects/masterview)

---

<div class="post-metadata">

**Author:** ![JSBSRainsberger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3da27b/32.png) [@JSBSRainsberger](https://www.ruby-forum.com/u/JSBSRainsberger)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/rails-view-as-pdf-suggestions/76124/6 "2006-11-20T14:46:19Z")

</div>

J. B. Rainsberger wrote:

> free to make wild suggestions. The only important constraint I can think  
> So, any suggestions? Any questions for me? Any choice Google keywords  
> you care to suggest? I could really use your help.

This is just a follow-up. I found a neat tool online, from FyTek, called  
“PDF Meld” that can overlay a PDF onto a graphic, which is perfect for  
our purposes. We’re now integrating its script into our Rails  
application, and if it works well, you’ll see me write about it in  
detail. If not, your ideas are a tremendous backup plan.

Thanks again.
