Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a PDF from a template in Ruby on Rails

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.

like image 624
Nicos Karalis Avatar asked Aug 14 '11 19:08

Nicos Karalis


People also ask

How do I use Ruby on Rails templates?

To apply a template, you need to provide the Rails generator with the location of the template you wish to apply using the -m option. This can either be a path to a file or a URL. You can use the app:template rails command to apply templates to an existing Rails application.

How do I read a PDF file in Ruby?

You could use JRuby and a Java PDF library parser such as ApachePDFBox (https://www.ohloh.net/p/pdfbox). See also http://java-source.net/open-source/pdf-libraries.

What are Ruby templates?

Template Method is a behavioral design pattern that allows you to defines a skeleton of an algorithm in a base class and let subclasses override the steps without changing the overall algorithm's structure.


1 Answers

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.

like image 161
dogenpunk Avatar answered Sep 28 '22 05:09

dogenpunk