/pd4ml-ruby

A Rails plugin wrapper for PD4ML, a commercial Java library for converting HTML to PDF

Primary LanguageRubyMIT LicenseMIT

PD4ML-Ruby

PD4ML-Ruby is a Ruby/Rails plugin that uses PD4ML, a commercial Java library
to convert HTML input files into formatted PDF.

For more information on PD4ML visit the website.

This is the initial release and may be buggy as hell. Your suggestions and
comments are most welcome.

Usage

Using PD4ML-Ruby is trivial. The example below illustrates a simple
use which generates a PDF file and show it inline to the browser:

html_content = <<-HTML <h1>Whoops!</h1> <p>Look at the stuff I am not doing!</p> HTML

Create the PDF object:

pdf = PD4ML.new( :bookmark_elements => ‘HEADINGS’, :allow_annotate => true, :allow_copy => false, :allow_modify => false, :allow_print => true )

Add the HTML content:

pdf << html_content

Set the user password, if needed:

pdf.user_password = “quentin”

Generate and send the file to the browser:

send_data(pdf.generate, :type => “application/pdf”, :filename => “hello.pdf”, :disposition => “inline” )

If you want to save the file to a folder on the server itself, say the code
was running in a batch job, you simply use the save_to method instead
of the generate method.

pdf.save_to “#{Rails.root}/public/hello.pdf”

You can set the options individually also, instead of passing them as a hash to the new
method:

pdf.set_option :allow_print, true pdf.set_option :html_width, 650

The default options are as follows:

{ :html_width => 800, :page_dimension => ‘A4’, :page_orientation => ‘PORTRAIT’, :inset_unit => ‘mm’, :inset_left => 20, :inset_top => 10, :inset_right => 10, :inset_bottom => 10, :bookmark_elements => ‘HEADINGS’, :allow_annotate => true, :allow_copy => true, :allow_modify => true, :allow_print => true, :debug => false }

For the detailed explanation of these options, refer to the
PD4ML documentation.

You can customize the paths for the various external dependencies. You will need
the java runtime, PD4ML library and fonts, if you are using an custom TTF fonts.

RAILS_ROOT/config/initializers/pd4ml-ruby.rb:

PD4ML.jar_path = “#{Rails.root}/extras/pd4ml/pd4ml.jar” PD4ML.java_path = “/usr/bin/java” PD4ML.font_path = “#{Rails.root}/extras/fonts”

Before using any fonts, you need to run this rake task:

rake pdf:build_fonts

This creates a pd4fonts.properties file in the fonts directory. This file is required for identifying font names.

TODO

  • Support for static and dynamic headers and footers
  • Lots of things

License

Copyright © 2009 Nilesh Chaudhari (mail at nilesh.org)

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.