5

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.

Nicos Karalis
  • 3,724
  • 4
  • 33
  • 62

3 Answers3

4

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.

dogenpunk
  • 4,332
  • 1
  • 21
  • 29
  • On second thought, you could create a Prawn::Text::Box that's the same color as your background then place that over your template tags. Then place the substituted text on top of that box. I don't believe there's really an easy way to do this. – dogenpunk Aug 18 '11 at 22:42
  • well ... This manual explains a bit about templates, but what I really wanted was to change the PDF without having to hacking on the code. And I do not think that text box will load the formatting from template. thanks anyway – Nicos Karalis Aug 19 '11 at 13:11
  • Yeah, from what I've found there's no good/easy way to do this. If you have a couple grand for the license there is a commercial library PrinceXML that apparently will convert XML/HTML with CSS to PDF. That may be the easiest way, but expensive. If your clients can create actual PDF Template you might check into this, http://forums.adobe.com/thread/740262 – dogenpunk Aug 19 '11 at 16:02
  • that won't help... the final user of this application doesn't now anything of xml/html/css. i found a way to change the xml inside a .odt file, but i'm still looking at it. – Nicos Karalis Aug 19 '11 at 17:26
  • Not being particularly familiar with .odt and .doc formats I can't say for sure, but there's probably a gem out there (or a native function of the programs that deal with them) that will convert them to html/xml with css. From there you could pretty easily do your text substitution and then pass it to PrinceXML. But again, ~$4000 for a server license may be the real barrier here. – dogenpunk Aug 19 '11 at 21:57
2

This may address the first half of the problem. Then use something like wicked_pdf to generate.

Tim Laseke
  • 21
  • 2
0

There is a really nice screencast from Ryan Bates here explaining the trick with PDF::Writer. I think that's what you want :)

Cydonia7
  • 3,744
  • 2
  • 23
  • 32
  • I have seen this screencast, but it does not fit in my needs – Nicos Karalis Aug 17 '11 at 11:54
  • you will make and save a document as a document (doc, odt, pdf, doesn't matter). will send it to me. I'll put in my rails application. my rails app will create a PDF using document as your template. User information with the my rails app will create a PDF and will send via email to the User. I do not know how to be more precise. - Client generates pdf. - Rails application the document fills in of the client with the User information and generates a pdf. I just need it, use a document that already exists, and build a new which I can change some information. – Nicos Karalis Aug 17 '11 at 13:40