Requirements:
PDF::Writer
sudo gem install pdf-writer
Installation and usage:
To install the plugin, use the following command:
ruby script/plugin install svn://rubyforge.org//var/svn/railspdfplugin/railspdf/
That will set everything up with the latest code.
To use the plugin, Grab the latest file and install it into the plugin directory of your Rails application.
The API for the pdf-writer tool is at: hxxp://ruby-pdf.rubyforge.org/pdf-writer/doc/index.html
To begin rendering PDF's, simply create a view with a .rpdf extension and paste in the code:
pdf.select_font "Times-Roman" pdf.text "Hello, World", :font_size => 72, :justification => :center
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. (Sample Code at the bottom)
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.
The default filename for the pdf is "controller_name.pdf" To override it, set an instance variable in your controller named "@rails_pdf_name" The rendered pdf will take this filename.
Other configurations instance variables you can use in your controller:
set @rails_pdf_inline to true to have the pdf display in the browser. set @paper to paper size, e.g. 'A4' 'LETTER' (default is 'LETTER') set @landscape to true to put the pdf in landscape mode rather than portrait.
Ok, I think that is all for now. Please let me know if you have any questions.
Contoller Code >
class PagesController? < ApplicationController? def index end def getpdf @rails_pdf_name = "Hello.pdf" @content = "This is dynamic content!!!" end
# Scott Wilson suggested adding the following two block so errors will always show in browser
def rescue_action_in_public(exception)
headers.delete("Content-Disposition")
super
end
def rescue_action_locally(exception)
headers.delete("Content-Disposition")
super
end
end
File: "getpdf.rpdf"
pdf.select_font "Times-Roman" pdf.text @content, :font_size => 72, :justification => :center
File: "index.rhtml"
Get yo fish and dynamic pdf's!<br /><br /> <%= link_to( "Get PDF", :controller => "pages", :action => "getpdf" )%>
Notes:
In order to get the plugin running in RoR? 1.2, it may be necessary to comment out the include requets for ApplicationHelper?, line 11 of railspdf.rb
If you comment out the above line then you won't have access to your view helpers. Try changing line 11 to read: "include ActionView?::Helpers::ApplicationHelper?" This is currently working for me in edge. YMMV.