Pdf with .doc templates using prawn in Ruby on Rails

Email this to someoneShare on Google+Share on FacebookTweet about this on TwitterShare on LinkedIn

You want to generate pdf documents in your Ruby on Rails app using .doc templates.

Open the .doc document in OpenOffice and save it as an Open Document Format (.odt).

Open the saved .odt document and export to pdf, making sure you use ‘Losless compression’ in the General Options section.

Use imagemagick’s convert to make the pdf file to an image (setting the density to your requirements – default is 72dpi):

convert -density 150 one.pdf one.png

Copy the image one.png to your rails image directory (public/images).

Use the image as a background to the pdf created by prawnto (install plugin if you don’t have it) with:

pdf.image(templ, {:position => :center, :vposition => :top, :scale => 0.215})

Put the database field(s) text on position (you’ll have to play around with the coordinates to get it right), like:

pdf.font “#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf”, :size => 9
pdf.text “#{@model_name.fielda_name}”, :at => [100,530]
pdf.text “#{@model_name.fieldb_name}”, :at => [100,510] …