Build a PDF from a template in Ruby on Rails

Nicos Karalis picture Nicos Karalis · Aug 14, 2011 · Viewed 7.4k times · Source

I would like to create a template in doc, pdf, odt, docx ... document type does not matter. What I want is to get this document, change some values ​​and convert this document to PDF.

For example:

Take this template:

<Title>
<Date>

Hello <Name>,

<Text>

         <Farewell>

And with this information:

Title: attention
Date: 22/05/2011
Name: NicosKaralis
Text: Lorem ipsum ...
Farewell: See you later

generate this pdf document, using color, background and anything that may have the template:

attention
22/05/2011

Hello NicosKaralis,

Lorem ipsum ....

         See you later.

There are any way to do this in Ruby on Rails? preferably using prawn

Thanks in advance

-- EDIT --

1 The owner of the application will create something like this: Original.(odt, doc, pdf)

2 My app database will have a user with this attributes:

title = Welcome to the internet!

name = Jhon Doe

text = Welcome to the internet, i will be your guide

farewell = Hope to see you soon!

3 My app will create a pdf like this: Final.pdf and send it by email

The email part i already have working. I just don`t have the pdf.

P.S.: I now how to create a pdf from scratch, but i dont want that. The point of this application is that you don`t need to be a rails developer to change the pdf that is generated.

Answer

dogenpunk picture dogenpunk · Aug 19, 2011

I'm not familiar with prawnto, but I'm pretty familiar with prawn. Since version 0.10.1 they've had the ability to import another pdf for use as a "template" per se. If you check the new manual there's a section on templates. If you look at the page titled templates/page_template.rb you'll see some example code. It takes some text and positions it manually, but there may be a way to do some sort of actual substitution.