1

In my Job Management application I would like to prepare an A4 page receipt to customer.

Thus, I need to print somehow Job model's details to a single A4 PDF page.

Are the any built-in tools in Rails for this purpose ?

If no, what would be the best way to go ?

Charles
  • 50,943
  • 13
  • 104
  • 142
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

2 Answers2

1

Theres a gem called prawn that helps with PDF generation. Here is a tutorial using it for some ideas:

http://railstips.org/blog/archives/2008/10/13/how-to-generate-pdfs-in-rails-with-prawn/

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
1

Basically there is two options: (any PDF creation requires a gem - no default PDF creation for rails).

  1. Create a pure PDF using Prawn, You have to do all the formatting using the Prawn API

  2. Create a HTML version of your receipt and convert it to PDF, One of the better gems to do this is PDFkit. which uses a web-kit powered browser engine.

They both work good, For one page documents I usually use PDFkit to convert HTML and for larger documents that are going have lots of pages I use Prawn because it gives you a smaller file size and handles multiple pages better.

My suggestion would be to make a HTML receipt and display it on the screen and give the user an option to save a PDF version using PDFkit.

EDIT: windows install. (not tested - windows and I have parted company.)

Download the windows installer for wkhtmltopdf: win-wkhtmltopdf

now create an initializer file, e.g. config/intializers/pdfkit_config.rb

in pdfkit_config.rb set the absolute path to wkhtmltopdf on your local machine:

PDFKit.configure do |config|
  if RAILS_ENV == 'development'
    config.wkhtmltopdf = 'D:\path\to\your\wkhtmltopdf' #this bit i'm not sure about
  else
    config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
  end
end

for your production ENV you can actually just have a copy of wkhtmltopdf in you repo, a unix version of course. (remember to chmod +x it before you git add it)

David Barlow
  • 4,914
  • 1
  • 28
  • 24
  • PDFkit really looks good and simple enough to my needs. But, the installation instructions says I need to install `wkhtmltopdf`, and I don't see how to install it on Windows: https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF. Any ideas ? – Misha Moroshko Mar 02 '11 at 07:06
  • Hi Misha, have edited my answer to include a method to try and get your dev env to run wkhtmltopdf. – David Barlow Mar 02 '11 at 07:26
  • Thanks a lot !! I did this, but unfortunately I experience another problem: http://stackoverflow.com/questions/5176239/rials-3-pdfkit-problem-permission-denied-errnoeacces – Misha Moroshko Mar 03 '11 at 03:12