DEV Community

Generate PDF invoices from Markdown using Pandoc

Martin Betz on January 09, 2017

TL;DR You can use Pandoc with WKHTMLTOPDF to generate nice-looking PDF invoices from Markdown files. The context I love simp...
Collapse
 
4ndrej profile image
Andrej

I prepare invoice data in my ruby web app and render pdf invoice using github.com/prawnpdf/prawn and github.com/prawnpdf/prawn-table ruby libraries.

The app manages the client database as well as the invoice item details and VAT summary. The Prawn could use the grid system similar to the Twitter Bootstrap so it's quite easy to do layout too.

Collapse
 
martin_betz profile image
Martin Betz

Thanks for your answers. Did you write a whole own app for your client and financial management? Or is it an add-on on a SaSS solution where you already have the customer data?

Collapse
 
4ndrej profile image
Andrej

Well, it's ultra minimalistic single purpose tool hacked together within few evenings and using for few years.

All customers / invoices data is in ruby source file so there is no need for CRUD code. Adding customer is just copy/paste in ruby file and editing few lines. Adding invoice is super easy copy/paste with just defining customer, date and invoice items. Good old copy/paste programming.

It's using Sinatra github.com/sinatra/sinatra which allows super short code. The main app which basically does just routing and params checking is under 80 lines including requires and some comments.

There is a few hundred lines of customers / invoices data too. It's growing quite slow since this is only side-job. All data are loaded into memory but I can spare few megabytes for the in memory data for sure.

The haml view files for rendering html app are under 4kB. Haml haml.info/ is saving lot of typing.

All magic happens in the invoice.prawn file which is 440 lines and about 11kB. There is the pdf layout filled with data from the customer and invoice data with lot of conditions controlling the optional data (for example VAT number is optional so I have to skip the space when there is no VAT number defined to retain the layout). There is also 15 lines dealing with preparing the VAT summary for 0/10/20% VAT per each item defined.

This prawn file generates the pdf which is shown in the Chrome so I can preview it and then save and send to customer.

Collapse
 
nimafazeli profile image
nimafazeli • Edited

Hi,

There are readily available letter templates for pandoc that you can customize, e.g this. Any reason to do it using css?

Just asking because I am new to all this and have no idea about css

Collapse
 
martin_betz profile image
Martin Betz

I want to customize the design of the output, so I will need CSS or TeX. I feel a lot more comfortable with CSS than with TeX. So I choose CSS.

Collapse
 
orschiro profile image
Robert Orzanna

Is this only a layout template or does it also automate the content, e.g. incrementing invoice number, using today's date, et cetera?

Collapse
 
martin_betz profile image
Martin Betz

Right now the markdown is static, so you have to edit invoice number, date, etc. on your own. The script is just transforming the text only to a layouted PDF.

Collapse
 
orschiro profile image
Robert Orzanna

Thank you!