Notes to self

InvoicePrinter: generate PDF invoices and receipts in seconds with pure Ruby

They are many options how to generate PDF versions of your invoices in Ruby and the ones that do not come with system dependencies are often built on top of Prawn library. InvoicePrinter is no different. Here is a short announcement on what it is (and aims to be) and what not.

I created InvoicePrinter to extract the mess of using Prawn directly in Rails views. The option of using plain Prawn was convenient, but once I started building some kind of view logic in views I stopped liking it. So I built this well-tested thin wrapper on top of Prawn to extract the code somewhere else. Actually last time I checked I am not the only one and there are other similar projects out there.

InvoicePrinter is different in two things. First, it does not impose on you anything. You don’t need to have I18n gem to do the translation of the labels. You don’t need to fight any kind of business logic of taxes for different tax system. It’s only a tested view layer, no other validations are part of it. And this is actually a feature. Second, it has its own unique standard design. I simply didn’t like the designs of others so I am offering an alternative. It might get some options to style borders or colors in future, but will most likely never support more layouts.

Anyway, here is how a simple invoice can look like: simple_invoice

And this is the code to generate it taken from the examples:

item = InvoicePrinter::Document::Item.new(
  name: 'Programming',
  quantity: '10',
  unit: 'hr',
  price: '$ 90',
  amount: '$ 900'
)

invoice = InvoicePrinter::Document.new(
  number: 'NO. 198900000001',
  provider_name: 'John White',
  provider_street: '5th Avenue',
  provider_street_number: '1',
  provider_postcode: '747 05',
  provider_city: 'NYC',
  purchaser_name: 'Will Black',
  purchaser_street: '7th Avenue',
  purchaser_street_number: '1',
  purchaser_postcode: '747 70',
  purchaser_city: 'NYC',
  issue_date: '05/03/2016',
  due_date: '19/03/2016',
  total: '$ 900',
  bank_account_number: '156546546465',
  items: [item]
)

InvoicePrinter.print(
  document: invoice,
  file_name: 'simple_invoice.pdf'
)

InvoicePrinter supports other stuff like custom fonts or logo, check the list of all features on the project page on GitHub.

And please let me know what you miss so I could add it before releasing a final 1.0.

Check out my book
Interested in Ruby on Rails default testing stack? Take Minitest and fixtures for a spin with my latest book.

Get Test Driving Rails and make your tests faster and easier to maintain.

by Josef Strzibny
RSS