├── .gitignore ├── MIT-LICENSE ├── README.rdoc ├── Rakefile ├── VERSION ├── init.rb ├── lib └── prawn_rails.rb ├── prawn_rails.gemspec └── rails └── init.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .redcar/* 2 | *.gem 3 | *~ 4 | doc/* 5 | .idea/* 6 | pkg/* 7 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Walton Hoops 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.rdoc: -------------------------------------------------------------------------------- 1 | = Prawn::Rails 2 | 3 | Prawn::Rails provides a simple way of creating PDF views in Rails 3 using the prawn library. 4 | 5 | To use Prawn::Rails simply add the line 6 | 7 | gem 'prawn_rails' 8 | 9 | to your Gemfile and then run 10 | 11 | bundle install 12 | 13 | That's it! You can now create views named 14 | 15 | [action].pdf.prawn 16 | 17 | which will be used whenever the user requests a page with a 'pdf' extension 18 | 19 | == Usage 20 | 21 | === Basic Usage 22 | 23 | Prawn::Rails is designed to provide only a very thin wrapper around Prawn itself. A Prawn::Rails view should consist of only a call to the function prawn_document and a block. This will create an instance of Prawn::Document and yield it to the block. 24 | For a simple pdf view try: 25 | 26 | views/.../simple.pdf.prawn 27 | 28 | prawn_document() do |pdf| 29 | pdf.text "Hello World" 30 | end 31 | 32 | This will create a simple PDF with only the text Hello World. 33 | 34 | === Partials 35 | 36 | While layouts do not yet work with Prawn::Rails, partials work fine. Rendering a partial is much like in a normal view. For example: 37 | 38 | views/.../partial.pdf.prawn 39 | 40 | prawn_document do |pdf| 41 | render "frontpage", :pdf => pdf 42 | pdf.text "something else" 43 | end 44 | 45 | views/.../_frontpage.pdf.prawn 46 | 47 | pdf.text "frontpage action!!" 48 | pdf.start_new_page 49 | 50 | As you might expect this will result in a pdf with a leading page. 51 | 52 | === Instance Variables 53 | 54 | Like normal Rails views, instance variables assigned in the controller are made available in the view. For example: 55 | 56 | home_controller.rb 57 | 58 | class HomeController < ApplicationController 59 | def index 60 | @people=['Jane','John','Jack'] 61 | end 62 | end 63 | 64 | views/.../index.pdf.prawn 65 | 66 | prawn_document(:page_layout => :landscape) do |pdf| 67 | @people.each {|person| pdf.text person} 68 | end 69 | 70 | This will produce a pdf with Jane, John, and Jack all written on seperate lines. 71 | 72 | === Rendering Options 73 | 74 | Notice we passed a hash into prawn_document. Any parameters placed in this hash will be passed to the constructor of Prawn::Document, with a few exceptions. The :renderer, :force_download, and :filename options will not be passed on, but instead will be used as described below. 75 | 76 | The :renderer option will be removed before creating the document and can be used to override the class to be used for rendering with a subclass of Prawn::Document like so: 77 | 78 | views/.../override.pdf.prawn 79 | 80 | prawn_document({:renderer => ApplicationHelper::Foo}) 81 | 82 | So for the view above you could have an application helper file that looks like: 83 | 84 | application_helper.rb 85 | 86 | module ApplicationHelper 87 | class Foo < Prawn::Document 88 | def initialize(opts={}) 89 | super 90 | text "Foo" 91 | text "Bar" 92 | end 93 | end 94 | end 95 | 96 | This would generate a canned report with just the lines Foo and Bar. 97 | 98 | === Force Saving 99 | 100 | The :force_download option makes the browser display a 'save as' dialog rather than attempting to display the content in browser (this is achieved by setting the Content-Dispoition header). 101 | Note: due to problems with the Acrobat Reader plugin, this defaults to true if the :filename option is used. 102 | 103 | views/.../saveas.pdf.prawn 104 | 105 | prawn_document(:force_download=>true) do |pdf| 106 | pdf.text "Hello World" 107 | end 108 | 109 | The above will cause a 'save as' dialog to appear, even in browsers with a PDF plugin. 110 | 111 | Finally is the :filename option. This allows you to override the default filename to something other than the name of the action. 112 | Note: You should include the .pdf extension in the filename. Prawn::Rails will not do this for you. 113 | 114 | views/.../filename.pdf.prawn 115 | 116 | prawn_document(:filename=>'Hello.pdf') do |pdf| 117 | pdf.text "Hello World" 118 | end 119 | 120 | This will result in the user being promted to download a file named 'Hello.pdf'. 121 | 122 | == Gotchas 123 | 124 | The one major gotcha at this point is that layouts do not work. Do not attempt to make an app/views/layouts/application.pdf.prawn. All your pdf views will quit. This is something I hope to fix in a later release. In the meantime I recommend using custom classes like the one above to achieve a similair effect. 125 | 126 | == Examples 127 | 128 | For examples see: http://prawn-rails-demo.herokuapp.com 129 | 130 | 131 | Copyright (c) 2010 Walton Hoops, released under the MIT license 132 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'jeweler' 3 | 4 | Jeweler::Tasks.new do |gem| 5 | gem.name = "prawn_rails" 6 | gem.author = "Walton Hoops" 7 | gem.email = "me@waltonhoops.com" 8 | gem.homepage = "http://github.com/Whoops/prawn-rails" 9 | gem.platform = Gem::Platform::RUBY 10 | gem.summary = "Integrates Prawn into Rails in a natural way" 11 | gem.description = "The prawn_rails gem provides a Prawn based view engine for creating PDFs with rails." 12 | gem.has_rdoc = false 13 | gem.extra_rdoc_files = ["README.rdoc"] 14 | gem.license = "MIT" 15 | gem.add_dependency('railties', '>=3.0.0') 16 | gem.add_dependency('prawn', '>=0.11.1') 17 | end 18 | Jeweler::RubygemsDotOrgTasks.new 19 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.0.12 -------------------------------------------------------------------------------- /init.rb: -------------------------------------------------------------------------------- 1 | require 'prawn_rails' 2 | -------------------------------------------------------------------------------- /lib/prawn_rails.rb: -------------------------------------------------------------------------------- 1 | require 'prawn' 2 | 3 | if Prawn::VERSION == "0.8.4" 4 | require 'prawn/layout' 5 | require 'prawn/security' 6 | end 7 | 8 | module Prawn 9 | module Rails 10 | 11 | module PrawnHelper 12 | 13 | def prawn_document(opts={}) 14 | download = opts.delete(:force_download) 15 | filename = opts.delete(:filename) 16 | pdf = (opts.delete(:renderer) || Prawn::Document).new(opts) 17 | yield pdf if block_given? 18 | 19 | disposition(download, filename) if (download || filename) 20 | 21 | pdf.render 22 | end 23 | 24 | def disposition(download, filename) 25 | download = true if (filename && download == nil) 26 | disposition = download ? "attachment;" : "inline;" 27 | disposition += " filename=\"#{filename}\"" if filename 28 | headers["Content-Disposition"] = disposition 29 | end 30 | 31 | end 32 | 33 | class TemplateHandler 34 | class_attribute :default_format 35 | self.default_format = :pdf 36 | 37 | def self.call(template, source = nil) 38 | if source 39 | "#{source.strip}" 40 | else 41 | "#{template.source.strip}" 42 | end 43 | end 44 | 45 | end 46 | 47 | end 48 | end 49 | 50 | Mime::Type.register_alias("application/pdf", :pdf) unless Mime::Type.lookup_by_extension(:pdf) 51 | ActionView::Template.register_template_handler(:prawn, Prawn::Rails::TemplateHandler) 52 | ActionView::Base.send(:include, Prawn::Rails::PrawnHelper) 53 | -------------------------------------------------------------------------------- /prawn_rails.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | # stub: prawn_rails 0.0.12 ruby lib 6 | 7 | Gem::Specification.new do |s| 8 | s.name = "prawn_rails".freeze 9 | s.version = "0.0.12" 10 | 11 | s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= 12 | s.require_paths = ["lib".freeze] 13 | s.authors = ["Walton Hoops".freeze] 14 | s.date = "2021-07-07" 15 | s.description = "The prawn_rails gem provides a Prawn based view engine for creating PDFs with rails.".freeze 16 | s.email = "me@waltonhoops.com".freeze 17 | s.extra_rdoc_files = [ 18 | "README.rdoc" 19 | ] 20 | s.files = [ 21 | "MIT-LICENSE", 22 | "README.rdoc", 23 | "Rakefile", 24 | "VERSION", 25 | "init.rb", 26 | "lib/prawn_rails.rb", 27 | "prawn_rails.gemspec", 28 | "rails/init.rb" 29 | ] 30 | s.homepage = "http://github.com/Whoops/prawn-rails".freeze 31 | s.licenses = ["MIT".freeze] 32 | s.rubygems_version = "3.2.21".freeze 33 | s.summary = "Integrates Prawn into Rails in a natural way".freeze 34 | 35 | if s.respond_to? :specification_version then 36 | s.specification_version = 4 37 | end 38 | 39 | if s.respond_to? :add_runtime_dependency then 40 | s.add_runtime_dependency(%q.freeze, [">= 3.0.0"]) 41 | s.add_runtime_dependency(%q.freeze, [">= 0.11.1"]) 42 | else 43 | s.add_dependency(%q.freeze, [">= 3.0.0"]) 44 | s.add_dependency(%q.freeze, [">= 0.11.1"]) 45 | end 46 | end 47 | 48 | -------------------------------------------------------------------------------- /rails/init.rb: -------------------------------------------------------------------------------- 1 | # Include hook code here 2 | require 'prawn_rails' --------------------------------------------------------------------------------