# Wicked PDF [![Build Status](https://secure.travis-ci.org/mileszs/wicked_pdf.png)](http://travis-ci.org/mileszs/wicked_pdf) ## A PDF generation plugin for Ruby on Rails Wicked PDF uses the shell utility [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff. _Wicked PDF has been verified to work on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails 3_ ### Installation First, be sure to install [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/). If your wkhtmltopdf executable is not on your webserver's path, configure it in an initializer: WickedPdf.config = { :exe_path => '/usr/local/bin/wkhtmltopdf' } Note that versions before 0.9.0 [have problems](http://code.google.com/p/wkhtmltopdf/issues/detail?id=82&q=vodnik) on some machines with reading/writing to streams. This plugin relies on streams to communicate with wkhtmltopdf. More information about [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) could be found [here](http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html). Next: script/plugin install git://github.com/mileszs/wicked_pdf.git script/generate wicked_pdf or add this to your Gemfile: gem 'wicked_pdf' You may also need to add Mime::Type.register "application/pdf", :pdf to config/initializers/mime_types.rb ### Basic Usage class ThingsController < ApplicationController def show respond_to do |format| format.html format.pdf do render :pdf => "file_name" end end end end ### Advanced Usage with all available options class ThingsController < ApplicationController def show respond_to do |format| format.html format.pdf do render :pdf => 'file_name', :disposition => 'attachment', # default 'inline' :template => 'things/show.pdf.erb', :file => "#{Rails.root}/files/foo.erb" :layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file :wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary :show_as_html => params[:debug].present?, # allow debuging based on url param :orientation => 'Landscape', # default Portrait :page_size => 'A4, Letter, ...', # default A4 :save_to_file => Rails.root.join('pdfs', "#{filename}.pdf"), :save_only => false, # depends on :save_to_file being set first :proxy => 'TEXT', :basic_auth => false # when true username & password are automatically sent from session :username => 'TEXT', :password => 'TEXT', :cover => 'URL', :dpi => 'dpi', :encoding => 'TEXT', :user_style_sheet => 'URL', :cookie => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format :post => ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format :redirect_delay => NUMBER, :zoom => FLOAT, :page_offset => NUMBER, :book => true, :default_header => true, :disable_javascript => false, :grayscale => true, :lowquality => true, :enable_plugins => true, :disable_internal_links => true, :disable_external_links => true, :print_media_type => true, :disable_smart_shrinking => true, :use_xserver => true, :no_background => true, :extra => '' # directly inserted into the command to wkhtmltopdf :margin => {:top => SIZE, # default 10 (mm) :bottom => SIZE, :left => SIZE, :right => SIZE}, :header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout :url => 'www.example.com', :locals => { :foo => @bar }}, :center => 'TEXT', :font_name => 'NAME', :font_size => SIZE, :left => 'TEXT', :right => 'TEXT', :spacing => REAL, :line => true, :content => 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string) :footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout :url => 'www.example.com', :locals => { :foo => @bar }}, :center => 'TEXT', :font_name => 'NAME', :font_size => SIZE, :left => 'TEXT', :right => 'TEXT', :spacing => REAL, :line => true, :content => 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string) :toc => {:font_name => "NAME", :depth => LEVEL, :header_text => "TEXT", :header_fs => SIZE, :l1_font_size => SIZE, :l2_font_size => SIZE, :l3_font_size => SIZE, :l4_font_size => SIZE, :l5_font_size => SIZE, :l6_font_size => SIZE, :l7_font_size => SIZE, :l1_indentation => NUM, :l2_indentation => NUM, :l3_indentation => NUM, :l4_indentation => NUM, :l5_indentation => NUM, :l6_indentation => NUM, :l7_indentation => NUM, :no_dots => true, :disable_links => true, :disable_back_links => true}, :outline => {:outline => true, :outline_depth => LEVEL} end end end end By default, it will render without a layout (:layout => false) and the template for the current controller and action. ### Super Advanced Usage ### If you need to just create a pdf and not display it: # create a pdf from a string pdf = WickedPdf.new.pdf_from_string('