Project

to_pdf

0.0
No commit activity in last 3 years
No release in over 3 years
Render rails actions as a PDF using PrinceXML.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.6.0

Runtime

>= 3.0.0
 Project Readme

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.