Generate PDF from Rails

39,965

Solution 1

The best I've seen so far is Prawn:

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"
Share:
39,965
Sebastian
Author by

Sebastian

Updated on February 21, 2020

Comments

  • Sebastian
    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?