GetJobber/prawnto

View on GitHub
README.rdoc

Summary

Maintainability
Test Coverage
= Prawnto (prawnto_2)

{<img src="https://secure.travis-ci.org/GetJobber/prawnto.png" />}[http://travis-ci.org/GetJobber/prawnto] {<img src="https://codeclimate.com/github/GetJobber/prawnto.png" />}[https://codeclimate.com/github/GetJobber/prawnto]

A rails plugin providing templating abilities for generating pdf files leveraging the kick-ass prawn library (http://prawn.majesticseacreature.com/)

This is my attempt to merge the various forks of the prawnto gem, and <b>update it for Rails 3.1+</b>. 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.


<b>I could use some help with the wiki.</b> If you find this helpful, and aren't up for contributing to the code, I would appreciate some help getting the docs in order.


<b>Note:</b> It has not been tested with earlier versions of Rails.

== Installation in Rails 3.1+

In your <code>Gemfile</code>:

  gem "prawnto_2", :require => "prawnto"

Then run:

  bundle install

Now make a view:

  <action name>.pdf.prawn

---

= Usage

More Details will be coming to {wiki}[http://github.com/getjobber/prawnto/wiki].

=== As View in Controller

Simply create additional views that end with <code>.pdf.prawn</code> and rails will use the Prawnto template handler to render it properly. The beta has combined the regular <code>.prawn</code> and <code>.prawn_dsl</code> extensions. Now you won't get tired of typing <code>pdf.method</code>, 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: "from@example.com"

    def email_with_attachment
      @var1 = 1
      attachments["hello_world.pdf"] = render("test/default_render", :format => :pdf)
      mail :subject => "Hello", :to => "test@test.com" do |format|
        format.text
      end
    end
  end

If you need something more complicated, you may want to consider using the <code>Prawnto::Render.to_string</code> method listed next to create your attachment content.

<b>Gotcha:</b> 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)

<b>Experimental!!! Will be changed</b>

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

<b>Gotcha:</b> Rails isn't always using the correct formatting. If you're having troubles, add the <code>.pdf</code> 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 <code>PdfHelper</code> 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

=== Running tests
Tests can be ran against different versions of Rails like so:

<code>
BUNDLE_GEMFILE=spec/gemfiles/Gemfile.rails-4.2.x bundle install
BUNDLE_GEMFILE=spec/gemfiles/Gemfile.rails-4.2.x bundle exec rspec spec
</code>


=== Asset generation
All test assets have been generated using ruby 2.4 and prawn 2.2.2

To generate your own assets for alternate versions, launch the rails server and use it to generate new assets. The naming structure is:
spec/assets/<ruby_version>/file_name-<prawn_version>.pdf

---

Copyright on Updates - Copyright (c) 2014 OctopusApp Inc. ({getjobber.com}[http://getjobber.com]), released under the MIT license

Original Copyright - Copyright (c) 2008 cracklabs.com, released under the MIT license