How to generate PDF invoices in Golang
Creator.NewInvoice returns a prebuilt invoice layout: title and logo, seller and buyer
addresses, an information table, a line items table, a totals table and note sections. You
fill in the values and draw it.
invoice := c.NewInvoice()
invoice.SetNumber("0001")
invoice.SetDate("28/07/2016")
invoice.SetSellerAddress(&creator.InvoiceAddress{Name: "John Doe", City: "Cambridge"})
invoice.SetBuyerAddress(&creator.InvoiceAddress{Name: "Jane Doe", City: "London"})
invoice.AddLine("Test product #1", "1", "$10", "$10")
invoice.SetSubtotal("$100.00")
invoice.AddTotalLine("Tax (10%)", "$10.00")
invoice.SetTotal("$115.00")
if err := c.Draw(invoice); err != nil {
return err
}Every value is a string and the invoice does no arithmetic. The subtotal, each total line and the total are three independent pieces of text, and nothing checks that they add up. Compute and format them yourself.
AddLine is variadic and does not count its arguments against the number of columns. One
value too many pushes the extra cell onto the next row and shifts every line after it; one
too few leaves the row short and the next line continues in it. Neither case returns an
error, so it only shows up in the output.
The section order is fixed. If you need a different arrangement, build it from tables and paragraphs, or use a template.
Restyling, column editing and the rest of the API are covered in the invoices guide, and there is a walkthrough on the UniDoc blog.