Project

pixelpress

0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
PDF printer
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Pixelpress

Modeled after ActionMailer, this gem allows you to render PDFs from HTML via Rails' templating engine. Additionally, you can preview your PDF templates via a supplied Rails engine during development.

Installation

Add Pixelpress to your application's Gemfile:

gem "pixelpress"

And then execute:

$ bundle

You'll also need WeasyPrint. It needs to be installed and added to your PATH, so that which weasyprint returns the location of the binary. Read their installation instructions here: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html#installation

Usage

Run the printer generator providing the name of your printer with methods to be generated:

rails generate pixelpress:printer NAME [method_name1 method_name2 ...] [options]

This creates the new printer in app/printers. If you run it the first time, it will also add an ApplicationPrinter and mount the Rails engine in your config/routes.rb file.

Example

$ rails g pixelpress:printer invoice customer_invoice delivery_document

will generate an app/printers/invoice_printer.rb file with this content:

class InvoicePrinter < ApplicationPrinter
  def customer_invoice
    # put your code here
  end

  def delivery_document
    # put your code here
  end
end

The command will also generate corresponding templates as .pdf.erb files located in app/views/printers/invoice/:

  • customer_invoice.pdf.erb
  • delivery_document.pdf.erb

You can preview your documents by running rails s and go to

http://localhost:3000/rails/printers

To use your printers in code, you can use them similarly to how you'd use mailers:

InvoicePrinter.customer_invoice.pdf # render a temporary pdf file
InvoicePrinter.customer_invoice.html # get the rendered document in html format

So you can send them to the client via a controller action:

class InvoicesController < ApplicationController
  def show
    document = InvoicePrinter.customer_invoice
    respond_to do |format|
      format.html { render html: document.html }
      format.pdf { send_data document.pdf.read, disposition: 'inline', type: 'application/pdf' }
    end
  end
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec 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/Alex/pixelpress.

License

The gem is available as open source under the terms of the MIT License.