Skip to content

barsoom/prawn_cocktail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PrawnCocktail

Ruby CI

Simple documents, templates and helpers on top of Prawn.

Because writing Prawn documents PHP 4 style is no fun.

If you're using this with Ruby on Rails, get PrawnCocktailRails.

Usage

Configuration

You can change where PrawnCocktail looks for its templates. This is the default (suitable for Ruby on Rails):

PrawnCocktail.template_root = "app/views/documents"

Document

The document class provides a data hash for the template, and optionally a filename:

class InvoiceDocument < PrawnCocktail::Document
  def initialize(invoice)
    @invoice = invoice
  end

  def filename
    "invoice_#{@invoice.id}.pdf"
  end

  private

  def data
    {
      number: @invoice.id,
      amount: @invoice.amount,
      customer: { name: @invoice.customer_name }
    }
  end
end

In Ruby on Rails, we suggest putting this in app/documents/invoice_document.rb. But anywhere is fine as long as the file is loaded, automatically or otherwise.

The document has render and render_file(name) methods, just like Prawn::Document.

The filename is not required. Other libraries like PrawnCocktailRails may make use of it.

The data value is passed to the template as a recursive OpenStruct-like object.

If the data becomes complex, you are advised to extract one or many builder classes like InvoiceDocument::Data and call those from data.

Template

Put the Prawn code in a template in the PrawnCocktail.template_root directory, named after the document, with a .pdf.rb extension.

The defaults (suitable for Ruby on Rails) would make that e.g. app/views/documents/invoice_document.pdf.rb.

meta page_size: "A4"

content do |data|
  text "Invoice #{data.number}"
  move_down 10
  text "Amount: #{data.amount}"
  move_down 10
  text data.customer.name
end

The meta declaration is optional. It takes a hash which will be passed to Prawn::Document.new. This is where you specify page_size, page_layout and such.

The content block will be passed the data from the document as a recursive OpenStruct-type object (actually a RecursiveClosedStruct utility class).

The content block will be rendered in the context of a Prawn::Document instance.

Helpers

You are advised to extract any complexity or shared code out of the template and into helpers:

# Document

class InvoiceDocument
  helper BaseDocumentHelper
  helper InvoiceDocumentHelper
end
# Template

content do |data|
  common_header
  text "Yo."
end
# Helper

module BaseDocumentHelper
  def common_header
    text "Document generated #{Time.now}."
  end
end

Any loaded module can be used as a helper, as long as its methods can run in the context of a Prawn document.

If you use PrawnCocktailRails, you can put modules in app/documents/helpers, e.g. app/documents/helpers/base_document_helper.rb, and they will be autoloaded.

Note that you must explicitly declare the helpers you want. No helpers are automatically included. You can declare base helpers in a base document class, though, and they will be inherited.

initialize_template

If you want to run some code in every template, use an initialize_template block:

class BaseDocument < PrawnCocktail::Document
  initialize_template do
    font "Courier"
  end
end

This is incidentally how helpers are implemented:

def self.helper(mod)
  initialize_template { extend mod }
end

You can have any number of template initializers, not just one.

Template initializers in a superclass are inherited by subclasses, and the subclass can add its own.

Installation

Add this line to your application's Gemfile:

gem 'prawn_cocktail'

And then execute:

$ bundle

Or install it yourself as:

$ gem install prawn_cocktail

License

Copyright (c) 2013 Barsoom AB

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Simple documents, templates and helpers on top of Prawn.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages