Skip to content

hughevans/to_pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to_pdf

Render rails actions as a PDF using PrinceXML or Wkhtmltopdf.

Installation

In your Gemfile (Rails >= 3.0):

gem 'to_pdf'

Usage

Create your PDF template like so:

app/views/jobs/show.pdf.haml

Then in your controller it’s just like responding to HTML or JSON etc.:

class JobController < ApplicationController
  respond_to :pdf

  def show
    @job = Job.find(params[:id])
    respond_with(@job)
  end
end

Don’t worry about a regular HTML document structure. Treat it more like XML (example in HAML):

.job
  .name
    = job.name

You can though define CSS at the top of the document (note that “body” rules will apply even though we have not defined that element explicitly in our markup):

%style
  :sass
    @page
      margin: 2cm
    body
      color: #000
      font-family: "DejaVu Sans"
    p
      margin: 0 0 1cm 0

Or link a stylesheet:

= stylesheet_link_tag 'pdf', :media => 'all'

.job
  .name
    = job.name

This will even work with the Rails 3.1 asset pipeline. However, in development you will need to add this line of configuration to your development.rb:

MyProject::Application.configure do
  ...

  config.to_pdf_asset_domain = 'http://myproject.dev'
end

Another configuration option available is the path to the PrinceXML executable. Again, this can optionally be set on a per environment basis:

MyProject::Application.configure do
  ...

  config.prince_xml_executable_path = '/opt/bin/prince'
end

Caveats

  • Only sends PDFs inline

  • If you wish to use a layout then it also requires a .pdf format, eg. application.pdf.haml

  • In order for css files to load images then config.prince_xml_asset_domain needs to be defined

Wkhtmltopdf

To use Wkhtmltopdf us must add the following additional configuration:

MyProject::Application.configure do
  ...

  config.to_pdf_renderer = :wkhtmltopdf
end

Maintainers

License

Copyright 2011 Hugh Evans

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Render rails actions as a PDF using PrinceXML.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages