A rails plugin providing templating abilities for generating pdf files leveraging the kick-ass prawn library (prawn.majesticseacreature.com/)
This is my attempt to merge the various forks of the prawnto gem, and update it for Rails 3.1+. I want to thank the many developers who's code I collected together for the gem. Main credit goes to the developer of the initial Prawnto plugin - smecsia.
I could use some help with the wiki. If you find this helpful, and aren't up for contributing to the code, I would appreciate some help getting the docs in order.
Note: It has not been tested with earlier versions of Rails.
Installation in Rails 3.1+
In your Gemfile:
gem "prawnto_2", :require => "prawnto"
Now make a view:
More Details will be coming to wiki.
As View in Controller
Simply create additional views that end with .pdf.prawn and rails will use the Prawnto template handler to render it properly. The beta has combined the regular .prawn and .prawn_dsl extensions. Now you won't get tired of typing pdf.method, but instance variables get used. There are a number of options available which are detailed in the wiki (or will be soon).
As Attachment to Email
You can render PDF's as an attachment to an email very simply with Prawnto.
class PdfEmailer < ActionMailer::Base default from: "email@example.com" def email_with_attachment @var1 = 1 attachments["hello_world.pdf"] = render("test/default_render", :format => :pdf) mail :subject => "Hello", :to => "firstname.lastname@example.org" do |format| format.text end end end
If you need something more complicated, you may want to consider using the Prawnto::Render.to_string method listed next to create your attachment content.
Gotcha: Something odd happens to the default format handling. You have to include the format block at the bottom, and specify the text, html, etc formats you want included on the body.
From Model (or anywhere else)
Experimental!!! Will be changed
Sometimes you need to be able to render a PDF from anywhere (Background process saving to the File, combining PDFs, etc), but you still want access to your helpers. This feature instantiates the most basic controller, and renders the template to a string for you to use. (more info coming to wiki soon)
class SuperModel ... def to_pdf @x = 1 Prawnto::ModelRenderer.to_string "test/default_render.pdf", self end end
Gotcha: Rails isn't always using the correct formatting. If you're having troubles, add the .pdf to the end of the requested template.
Tips & Tricks
Helper methods can be called from within the PDF views. To help keep PDF's DRY, I would recommend creating a PdfHelper class for complex functionality. Don't forget to add this helper to your emailer if you generate PDF's as attachments.
Limited partial support is available. Check out the wiki for details.
Testing & Development
Tests can be ran against different versions of Rails like so:
BUNDLE_GEMFILE=spec/gemfiles/Gemfile.rails-3.2.x bundle install BUNDLE_GEMFILE=spec/gemfiles/Gemfile.rails-3.2.x bundle exec rspec spec
Copyright on Updates - Copyright © 2014 OctopusApp Inc. (getjobber.com), released under the MIT license
Original Copyright - Copyright © 2008 cracklabs.com, released under the MIT license