Recently I have worked on project that needed a pdf form to be filled with data gathered in Rails3 app db.
As I have decided to use PRAWN as a PDF generator (with prawn_rails gem – I can really recommend, works with Rails3), I had to choose between recreating the form or just filling in the form that was ready. As time=money, I decided to go for the second possibility.
1. I converted the pdf form to JPG (300 dpi resolution, no compression).
2. I needed A4 page size, so I also need to scale my image. In my example_document.pdf.prawn, I should put:
bg = "#{RAILS_ROOT}/public/pdf/pdf_bg.jpg" prawn_document ( :filename=>'foo.pdf', :page_size=> "A4", :margin => 0) do |pdf| pdf.image bg, :at => [0, Prawn::Document::PageGeometry::SIZES["A4"][1]], :fit => Prawn::Document::PageGeometry::SIZES["A4"] pdf.text "This is pdf with background image ready to fill in" end
The code above imports the image and positions it in the right left corner (remember! bounding box in pdf has its origin [0,0] in BOTTOM LEFT corner of the page). The size of A4 is set as constant by PRAWN (nice of them, isn’t it?). You can access it by Prawn::Document::PageGeometry::SIZES array. The full list of available page sizes you can find in PRAWN source code at https://github.com/sandal/prawn/blob/master/lib/prawn/document/page_geometry.rb.
3. Now one has to play around a little bit in order to position the data obtained from db in right positions.
Good luck!