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'
url = 'http://www.google.com'
url_generator = PhantomPDF::Generator.new(url, output_file, options)
returned_file = url_generator.generate
file = 'file://path/to/file.html'
file_generator = PhantomPDF::Generator.new(file, output_file, options)
returned_file = file_generator.generate
html = '<h1>Hello, PhantomPDF!</h1>'
html_generator = PhantomPDF::Generator.new(html, output_file, options)
returned_file = html_generator.generate
url_generator.generate('/tmp/dynamic_phantompdf_google.pdf')
Configuration
You can configure PhantomPDF globally in your project, such as Rails' initializers principle.
PhantomPDF.configure do |config|
:format => 'A4',
:header => nil,
:footer => nil,
:margin => '1cm',
:orientation => 'portrait',
:zoom => 1,
:cookies => {},
:timeout => 90000,
:rendering_timeout => 1000
end
You can also configure PhantomPDF in place when needs.
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
PhantomPDF::Generator.new(input, output=nil, options={})
PhantomPDF::Generator
PhantomPDF::Generator
PhantomPDF::Generator
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