Project

phantompdf

0.01
No commit activity in last 3 years
No release in over 3 years
Generate PDF from HTML using PhantomJS
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 9.1
~> 0.7
~> 3.6

Runtime

~> 2.1
~> 1.9
~> 2.0
 Project Readme

PhantomPDF

Generate PDF from HTML using PhantomJS!

Supporting URL, FILE and STRING formats for HTML resource.

Why PhantomPDF?

Within the Ruby community, there is no simply way to generate PDF from HTML. You must setup dependences separate and then call some wrapped methods around. When you manage several servers or need to do migrations around, things become much more terrible!

PhantomPDF was born to simplify those. It brings simple API and easy maintenance.

Yes, PhantomPDF is simply! You only need to take care of your HTML layout. And then put remaining work to PhantomPDF.

How to start?

Installation

For Gemfile project

Adding following to your Gemfile

gem 'phantompdf'

and then execute

$ bundle
For normal project
$ gem install phantompdf

That's all! Pretty simple?!

Usage

Quick starts
require 'phantompdf'

options = {
  :format => 'A4',
  :margin => '1cm'
}
output_file = '/tmp/phantompdf_google.pdf'

# for *URL* resource
url = 'http://www.google.com'
url_generator = PhantomPDF::Generator.new(url, output_file, options)
returned_file = url_generator.generate  # If output_file is valid(writable) returned_file == output_file, otherwise returned_file should be temp file of your OS.

# for *FILE* resource
file = 'file://path/to/file.html'
file_generator = PhantomPDF::Generator.new(file, output_file, options)
returned_file = file_generator.generate

# for *STRING* resource
html = '<h1>Hello, PhantomPDF!</h1>'
html_generator = PhantomPDF::Generator.new(html, output_file, options)
returned_file = html_generator.generate

# want to dynamic output?
url_generator.generate('/tmp/dynamic_phantompdf_google.pdf')  # This will output pdf file to /tmp/dynamic_phantompdf_google.pdf though you pass *output* arg when creating generator instance.
Configuration

You can configure PhantomPDF globally in your project, such as Rails' initializers principle.

PhantomPDF.configure do |config|
  # default pdf output format, e.g. "5in*7.5in", "10cm*20cm", "A4", "Letter"
  :format            => 'A4',

  # default pdf header, formatted in [headerHeight*]headerString(HTML is supported)
  # default to 1.2cm when you omit headerHeight
  # you can use *%{pageNum}* and *%{pageTotal}* to refer current values of page
  # example: 1.2cm*<h5>PhantomPDF header %{pageNum}/%{pageTotal}
  :header            => nil,

  # default pdf footer, formatted in footerHeight*footerString(HTML is supported)
  # you can use *%{pageNum}* and *%{pageTotal}* to refer current values of page
  # example: 0.6cm*<h5>PhantomPDF header %{pageNum}/%{pageTotal}
  :footer            => nil,

  # default page margin
  :margin            => '1cm',

  # default page orientation, 'portrait' or 'landscape'
  :orientation       => 'portrait',

  # default page zoom factor
  :zoom              => 1,

  # default cookies, only used for *URL* resource
  :cookies           => {},

  # PhantomJS running timeout
  :timeout           => 90000,

  # pdf rendering timeout, increase if your HTML page is big
  :rendering_timeout => 1000
end

You can also configure PhantomPDF in place when needs.

# for *URL* resource
url = 'http://www.google.com'
url_generator = PhantomPDF::Generator.new(url, output_file, options)
url_generator.options.header = '0.8cm*<h3>Configure PhantomPDF dynamic</h3>'
returned_file = url_generator.generate
APIs
# create PhantomPDF
PhantomPDF::Generator.new(input, output=nil, options={})

# generate pdf
PhantomPDF::Generator#generate(output=>nil)

# raise PhantomPDF::RenderingError when failed to generate pdf
PhantomPDF::Generator#generate!(output=>nil)

# get generated pdf as string
PhantomPDF::Generator#to_string

Contributing

If you'd like to help improve PhantomPDF or you find some bugs. You can contribute to it by following:

  • Submit an issue describing your problem
  • Fork your repo and work your magic
  • Test your code as far as possible
  • Submit a pull request when you finished hack

License

PhantomPDF is released under MIT license.

Authors

Spring MC