Invoices
On this page
Expand Collapse This guide will show you how to create well-formatted invoices on the go using UniPDF, a Golang PDF library.
UniPDF can also be used to generate personalized invoices with custom styling and formatting.
Before you begin # You should get your API key from your UniCloud account.
If you are using UniPDF SDK for the first time, follow this guide to set up a local development environment.
Clone the project repository # Clone the examples repository in your terminal. It contains the sample Golang code we’ll be using in this guide.
git clone https://github.com/unidoc/unipdf-examples.git
Navigate to the invoice
folder in the unipdf-examples directory.
cd unipdf-examples/invoice
How it works #
package main
import (
"fmt"
"log"
"os"
"github.com/unidoc/unipdf/v4/common/license"
"github.com/unidoc/unipdf/v4/creator"
)
func init () {
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY` ))
if err != nil {
panic (err)
}
}
func main () {
c := creator.New()
c.NewPage()
logo, err := c.NewImageFromFile("unidoc-logo.png" )
if err != nil {
log.Fatalf("Fail: %v\n" , err)
}
invoice := c.NewInvoice()
invoice.SetLogo(logo)
invoice.SetNumber("0001" )
invoice.SetDate("28/07/2016" )
invoice.SetDueDate("28/07/2016" )
invoice.AddInfo("Payment terms" , "Due on receipt" )
invoice.AddInfo("Paid" , "No" )
invoice.SetSellerAddress(&creator.InvoiceAddress{
Name: "John Doe" ,
Street: "8 Elm Street" ,
City: "Cambridge" ,
Zip: "CB14DH" ,
Country: "United Kingdom" ,
Phone: "xxx-xxx-xxxx" ,
Email: "johndoe@email.com" ,
})
invoice.SetBuyerAddress(&creator.InvoiceAddress{
Name: "Jane Doe" ,
Street: "9 Elm Street" ,
City: "London" ,
Zip: "LB15FH" ,
Country: "United Kingdom" ,
Phone: "xxx-xxx-xxxx" ,
Email: "janedoe@email.com" ,
})
for i := 0 ; i < 75 ; i++ {
invoice.AddLine(
fmt.Sprintf("Test product #%d" , i+1 ),
"1" ,
"$10" ,
"$10" ,
)
}
invoice.SetSubtotal("$100.00" )
invoice.AddTotalLine("Tax (10%)" , "$10.00" )
invoice.AddTotalLine("Shipping" , "$5.00" )
invoice.SetTotal("$115.00" )
invoice.SetNotes("Notes" , "Thank you for your business." )
invoice.SetTerms("Terms and conditions" , "Full refund for 60 days after purchase." )
if err = c.Draw(invoice); err != nil {
log.Fatalf("Error drawing: %v" , err)
}
err = c.WriteToFile("invoice_simple.pdf" )
if err != nil {
log.Fatalf("Fail: %v\n" , err)
}
}
Lines 9-16
import the UniPDF packages and other required dependencies.
The init function in lines 18-25
authenticates your request using your UNIDOC_LICENSE_API_KEY
.
In lines 27–98
, the main function creates a new instance of the PDF creator and calls the NewInvoice()
method on that instance.
This is where you set the information that will appear on the invoice, such as the logo, addresses, items, and their prices. You can also add more information and terms and conditions to the invoice. The created invoice is saved as a PDF document in your current directory.
Run the code # Run this command to generate a PDF invoice. This will also get all the required dependencies to run the program.
go run pdf_invoice_simple.go
Sample output #