BreezyPDF
Make PDF generation a breezy-easy.
No binaries to install. No reinventing the wheel to match HTML layouts. Just simple HTML to PDF generation as a Rack Middleware with support for modern CSS and JavaScript. .pdf
any of your app's URL's for fast, out-of-process, PDF generation of that URL. Support for public and authenticated views, local development and production.
No libraries to install.
No fonts to manage.
No DSL to learn.
Works anywhere.
Leverage modern CSS and ECMAScript.
Magical support for Sinatra, Ruby on Rails, and any other Rack based web framework.
Sign Up for an account to get started.
View the Demo and the Demo's Source Code
Installation
Add this line to your application's Gemfile:
gem 'breezy_pdf'
And then execute:
$ bundle
Or install it yourself as:
$ gem install breezy_pdf
Usage - Ruby on Rails
To add the middleware:
BreezyPDF.secret_api_key = "YOUR_SECRET_API_KEY"
config.middleware.use BreezyPDF::Middleware
<%= link_to "Download as PDF", my_resource_path(format: :pdf) %>
Proceed to configuration.
Usage - Sinatra/Hamani/Rack
To add the middleware:
BreezyPDF.secret_api_key = "YOUR_SECRET_API_KEY"
use BreezyPDF::Middleware
<a href="/this/url.pdf">Download as PDF</a>
Proceed to configuration.
Configuration
BreezyPDF supports extensive configuration for how want PDF's to render:
BreezyPDF.setup do |config|
config.secret_api_key = "YOUR_SECRET_API_KEY"
end
Where's my loading animation?
By default, the requested PDF will eventually returned by the request after a series of redirects. The PDF will be sent with a Content-Disposition of attachment
, so the browser will attempt to download it instead of showing it inline. All the while, and after the PDF has been downloaded, the current page's HTML will continue to be displayed.
If you want to show a loading animation on the current page, you can simply load the PDF with an AJAX request, eventually redirecting to the final URL.
Here is a contrived example:
<a href="/this/path.pdf" class="pdf-link">Download as PDF</a>
<script type="text/javascript">
$('.pdf-link').on('click', function(clickEv) {
var startTime = new Date();
var targetEl = $(clickEv.target);
var modalEl = $('#pdf-loading');
var ajaxRequest = new XMLHttpRequest();
clickEv.preventDefault();
ajaxRequest.addEventListener('load', function(ev) {
modalEl.modal('hide');
window.location = ev.currentTarget.responseURL;
})
modalEl.modal('show');
ajaxRequest.open('GET', targetEl.attr('href'));
ajaxRequest.send();
})
</script>
Pragmatic access
You may want to render a PDF out of the HTTP request cycle like in a background job or mailer. BreezyPDF supports rendering HTML as a PDF pragmatically. BreezyPDF::HTML2PDF expects two arguments, the asset_host
for assets, and the html fragment. The asset_host
should be the locally accessible host where external assets (js, css) can be downloaded from. In development, this may be http://localhost:3000
, while in production it may be your configured Rails.application.config.action_controller.asset_host
. If BreezyPDF.upload_assets == false
, then this value has no impact.
Here is an example of how you might do that:
def invoice_mailer(user, invoice)
asset_host = Rails.env.production? ? Rails.application.config.action_controller.asset_host : "http://localhost:3000"
metadata = { width: 8.5, width: 11 }
renderer = ApplicationController.renderer.new(https: true)
html = renderer.render(template: "invoices/show", assigns: { invoice: invoice }, locals: { current_user: user })
pdf = BreezyPDF::HTML2PDF.new(asset_host, html, metadata)
attachments["invoice-#{invoice.id}.pdf"] = pdf.to_file.read
@pdf_url = pdf.to_url
end
This example uses ActionController::Renderer to render a controller template's html.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/danielwestendorf/breezy_pdf.
License
Copyright (c) Daniel Westendorf
BreezyPDF is an Open Source project licensed under the terms of
the LGPLv3 license. Please see http://www.gnu.org/licenses/lgpl-3.0.html
for license text.