├── init.rb ├── CHANGELOG ├── test ├── test_helper.rb └── railspdf_test.rb ├── Rakefile ├── lib └── railspdf.rb └── README /init.rb: -------------------------------------------------------------------------------- 1 | require 'railspdf' 2 | 3 | ActionView::Template.register_template_handler 'rpdf', RailsPDF::PDFRender -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | * Fixed case where matching view helper might not be available. [Matthew Bass] 2 | 3 | * Now compatible with Rails 2.1.0 [Matthew Bass] 4 | 5 | * Forked project on GitHub [Matthew Bass] -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'action_controller' 3 | require 'test/unit' 4 | 5 | module ApplicationHelper; end 6 | 7 | $LOAD_PATH << File.dirname(__FILE__) + '/../lib' 8 | require 'railspdf' 9 | require 'mocha' -------------------------------------------------------------------------------- /test/railspdf_test.rb: -------------------------------------------------------------------------------- 1 | require File.dirname(__FILE__) + '/test_helper' 2 | 3 | class SomeController < ActionController::Base; end 4 | module SomeHelper; end 5 | 6 | class AnotherController < ActionController::Base; end 7 | 8 | class RailsPDF::PDFRenderTest < Test::Unit::TestCase 9 | 10 | def test_should_not_barf_on_missing_helper 11 | view = stub(:controller => AnotherController.new) 12 | RailsPDF::PDFRender.new(view) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rake' 2 | require 'rake/testtask' 3 | require 'rake/rdoctask' 4 | 5 | desc 'Default: run unit tests.' 6 | task :default => :test 7 | 8 | desc 'Test the railspdf plugin.' 9 | Rake::TestTask.new(:test) do |t| 10 | t.libs << 'lib' 11 | t.pattern = 'test/**/*_test.rb' 12 | t.verbose = true 13 | end 14 | 15 | desc 'Generate documentation for the railspdf plugin.' 16 | Rake::RDocTask.new(:rdoc) do |rdoc| 17 | rdoc.rdoc_dir = 'rdoc' 18 | rdoc.title = 'Railspdf' 19 | rdoc.options << '--line-numbers --inline-source' 20 | rdoc.rdoc_files.include('README') 21 | rdoc.rdoc_files.include('lib/**/*.rb') 22 | end 23 | -------------------------------------------------------------------------------- /lib/railspdf.rb: -------------------------------------------------------------------------------- 1 | require 'pdf/writer' 2 | require 'pdf/simpletable' 3 | 4 | module RailsPDF 5 | 6 | #this code comes from http://wiki.rubyonrails.com/rails/pages/HowtoGeneratePDFs 7 | class PDFRender < ActionView::Base 8 | include ApplicationHelper 9 | 10 | def initialize(action_view) 11 | @action_view = action_view 12 | helper_name = "#{action_view.controller.controller_name.classify}Helper" 13 | self.class.send(:include, helper_name.constantize) if Object.const_defined?(helper_name) 14 | end 15 | 16 | def render(template, local_assigns = {}) 17 | 18 | #get the instance variables setup 19 | @action_view.controller.instance_variables.each do |v| 20 | instance_variable_set(v, @action_view.controller.instance_variable_get(v)) 21 | end 22 | 23 | #keep ie happy 24 | if @action_view.controller.request.env['HTTP_USER_AGENT'] =~ /msie/i 25 | @action_view.controller.headers['Pragma'] ||= '' 26 | @action_view.controller.headers['Cache-Control'] ||= '' 27 | else 28 | @action_view.controller.headers['Pragma'] ||= 'no-cache' 29 | @action_view.controller.headers['Cache-Control'] ||= 'no-cache, must-revalidate' 30 | end 31 | 32 | @action_view.controller.headers["Content-Type"] ||= 'application/pdf' 33 | if @rails_pdf_name 34 | @action_view.controller.headers["Content-Disposition"] ||= 'attachment; filename="' + @rails_pdf_name + '"' 35 | elsif @rails_pdf_inline 36 | #set no headers 37 | else #rails_pdf_inline was set to false. set filename = controller name 38 | #since we weren't provided a custom name 39 | @action_view.controller.headers["Content-Disposition"] ||= 'attachment; filename="' + @action_view.controller.controller_name + '.pdf' + '"' 40 | end 41 | 42 | if @landscape 43 | pdf = PDF::Writer.new( :paper => (@paper || 'A4'), :orientation => :landscape ) 44 | else 45 | pdf = PDF::Writer.new( :paper => (@paper || 'A4') ) 46 | end 47 | pdf.compressed = true if RAILS_ENV != 'development' 48 | 49 | eval template.source, nil 50 | pdf.render 51 | end 52 | 53 | def self.call(template) 54 | "RailsPDF::PDFRender.new(self).render(template, local_assigns)" 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | railspdf 2 | 3 | Adds pdf-writer support to Rails 2.2 4 | 5 | 6 | Usage 7 | ----- 8 | 9 | Before going further, do not forget to add the following line in one of our initializers file: 10 | 11 | ActionView::Template.register_template_handler 'rpdf', RailsPDF::PDFRender 12 | 13 | To begin rendering PDFs, simply create a view with a .rpdf extension and paste in the code: 14 | 15 | pdf.select_font "Times-Roman" 16 | pdf.text "Hello, World", :font_size => 72, :justification => :center 17 | 18 | If you want the text to be dynamic, simply replace "Hello World" with an instance variable. It works like a charm, although I had to rearrange the code a bit to make it work. (See the sample controller code at the bottom.) 19 | 20 | I've not yet tested any of this; I was just too excited once it started working. Note: to get plugins to work properly, you MUST restart the server after installing it. 21 | 22 | **Important** If you are using a layout, you must disable it for the view!!! 23 | 24 | The default filename for the pdf is "Default.pdf" I'll probably change that later to reflect the view name, but for now it works pretty good. To override it, set an instance variable in your controller named "@rails_pdf_name" The rendered pdf will take this filename. 25 | 26 | Please let me know if you have any questions. 27 | 28 | 29 | Sample Controller 30 | ----------------- 31 | 32 | class PagesController < ApplicationController 33 | def getpdf 34 | @rails_pdf_name = "Hello.pdf" 35 | @content = "This is dynamic content!!!" 36 | end 37 | end 38 | 39 | Another way 40 | ----------- 41 | 42 | class OrdersController < ApplicationController 43 | def show 44 | @order = Order.find(params[:id]) 45 | 46 | respond_to do |format| 47 | format.html 48 | format.pdf do 49 | @rails_pdf_name = "order_#{@order.token}.pdf" 50 | render :layout => false 51 | end 52 | end 53 | end 54 | end 55 | 56 | Note: you have to register the mime-type for pdf files. The line below has to be added to your config/initializer/mime_types.rb file: 57 | 58 | Mime::Type.register "application/pdf", :pdf 59 | 60 | To render the link in your views to generate the pdf, just do this: 61 | 62 | <%= link_to 'pdf', formatted_order_path(order, :pdf) %> 63 | 64 | Sample View 65 | ----------- 66 | 67 | pdf.select_font "Times-Roman" 68 | pdf.text @content, :font_size => 72, :justification => :center 69 | 70 | 71 | Misc 72 | ---- 73 | 74 | Original RubyForge project (outdated): 75 | http://rubyforge.org/projects/railspdfplugin/ 76 | 77 | GitHub fork: 78 | http://github.com/pelargir/railspdf/ 79 | 80 | Clone URL: 81 | git://github.com/pelargir/railspdf.git 82 | 83 | 84 | Credit 85 | ------ 86 | 87 | * Created by Tom Willett 88 | * Forked and updated by Matthew Bass 89 | --------------------------------------------------------------------------------