Giter Site home page Giter Site logo

prawn-rails's Introduction

Prawn-Rails

Gem Version CI Status RubyGems Downloads

Install

gem 'prawn-rails'

Note: prawn and prawn-table are dependencies of prawn-rails so there is no need to mention it in the projects Gemfile unless you want to use a specific version of either of those libraries.

Usage

Create a view with pdf as format and prawn as handler so filename should look like example.pdf.prawn.

It provides a helper called prawn_document which builds a PrawnRails::Document with default options. You can override any options as you please. Example:

prawn_document do |pdf|
  pdf.text "Hello World"
end

No need to call pdf.render, it is called by prawn_document.

You can customize the name of the generated PDF and the file's delivery format with the :filename and :disposition keys:

prawn_document(filename: "my-file.pdf", disposition: "attachment") do |pdf|
  pdf.text "Direct download incoming!"
end

You can also override the content disposition using the Content-Disposition HTTP header. If you've already set this, prawn-rails will not override it. Here's an example of setting the header directly:

def download
  respond_to do |format|
    format.pdf do
      headers["Content-Disposition"] = "attachment; filename=\"charts-#{Time.now.to_i}.pdf\""
    end
  end
end

You can also override the file's name from the controller via @filename:

def show
  @filename = "my_report.pdf"
end

If no options are given, the file's name will match to your browser's default and the delivery format will default to inline.

Default configuration

Add a prawn-rails.rb config to your Rails app under config/initializers like this:

PrawnRails.config do |config|
  config.page_layout = :portrait
  config.page_size   = "A4"
  config.skip_page_creation = false
end

Please note that these are the defaults.

For a list of all available options: http://www.rubydoc.info/gems/prawn/Prawn%2FDocument:initialize.

For a list of all metadata the the :info option supports, please see https://github.com/prawnpdf/prawn/blob/master/manual/document_and_page_options/metadata.rb.

If skip_page_creation is set to true then you have to create the first page yourself. Example:

pdf.start_new_page
# OR
pdf.start_new_page size: "A4", page_layout: :portrait

Examples

Hello World

# hello.pdf.prawn

prawn_document do |pdf|
  pdf.text 'Hello World!'
end

Using Active Record

# myproducts.pdf.prawn

prawn_document do |pdf|
  pdf.text 'Current Products are:'
  pdf.move_down 20
  pdf.table @products.collect{|p| [p.name,p.price]}
end

Using Custom options

# landscape.pdf.prawn

prawn_document(page_layout: :landscape) do |pdf|
  pdf.text 'Landscape Page!'
end

Credits

Maintained by Weston Ganger - @westonganger

Created by Carlos Ortiz - @cortiz

prawn-rails's People

Contributors

westonganger avatar cortiz avatar atmartin avatar deepak avatar iffyuva avatar gaffneyc avatar smber1 avatar langalex avatar cgunther avatar stats avatar tilsammans avatar codinganarchy avatar sigmike avatar rwilliams avatar tatey avatar

Watchers

James Cloos 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.