Generate PDF from Rails
Solution 1
The best I've seen so far is Prawn:
- Prawn http://prawn.majesticseacreature.com/
- http://advent2008.hackruby.com/past/2008/12/17/awn_and_the_future_of_ruby_pdf_generation/
- http://railstips.org/2008/10/14/how-to-generate-pdfs-in-rails-with-prawn
- Prawn Rails plugin: http://www.cracklabs.com/prawnto
Solution 2
Prawn with Prawnto for sure. The DSL is a real treat, as is the simplicity of being able to treat PDF as any other format in a respond_to format block:
respond_to do |format|
format.pdf { render :layout => false }
There's a tutorial video on Prawn here:
Solution 3
There's also RTeX. That works well if you're willing to translate to LaTeX first. LaTeX is a very good way to store marked-up documents. It just depends on how static each document is. If most of the document is dynamic, you might do better with Prawn or PDF::Writer. If most of it is static, with just a couple of text-replacements for each, LaTeX might be a better choice.
Solution 4
There is also PDFKit. It's quite interesting too.
Solution 5
Prawn is the way to go. Now with prawn-labels that is really easy to do.
Check out the project's README here:
https://github.com/jordanbyron/prawn-labels#readme
This is a super simple example being used in a Rails controller. Don't forget to add gem 'prawn-labels'
to your Gemfile.
names = %w{Jordan Kelly Greg Bob}
labels = Prawn::Labels.render(names, :type => "Avery5160") do |pdf, name|
pdf.text name
end
send_data labels, :filename => "names.pdf", :type => "application/pdf"
Sebastian
Updated on February 21, 2020Comments
-
Sebastian over 4 years
The Ruby On Rails Wiki lists a couple of libraries that facilitate PDF generation in Rails. I need to print out address labels (in letter format, thus 12-15 addresses per page) and cannot decide which one to use. Any recommendations?