Giter Club home page Giter Club logo

prawnto's Introduction

Prawnto (prawnto_2)

<img src=“https://secure.travis-ci.org/GetJobber/prawnto.png” /> <img src=“https://codeclimate.com/github/GetJobber/prawnto.png” />

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"

Then run:

bundle install

Now make a view:

<action name>.pdf.prawn

Usage

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 protected]"

  def email_with_attachment
    @var1 = 1
    attachments["hello_world.pdf"] = render("test/default_render", :format => :pdf)
    mail :subject => "Hello", :to => "[email protected]" 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

Running tests

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

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

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 © 2014 OctopusApp Inc. (getjobber.com), released under the MIT license

Original Copyright - Copyright © 2008 cracklabs.com, released under the MIT license

prawnto's People

Contributors

andrewjdmccann avatar blaxter avatar c-flatla avatar christophebelpaire avatar djnawara avatar forrest avatar harking avatar huerlisi avatar jeanmartin avatar jessevanherk avatar jlsync avatar johnhampton avatar keeperpat avatar mdotterer avatar mocoso avatar nathany avatar niquola avatar pelargir avatar ptzn avatar rylwin avatar tjkruitbosch avatar tomstuart avatar tra avatar vfrride avatar wigsgiw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.