Documentation ¶
Overview ¶
Package pdfinvoice is a library for generating PDF invoices.
Defining the required environment variables ¶
The following environment variables are required:
`FONT_PATH` - the path to the font files to be used `LOGO_FILE_PATH` - the path to the logo file to be used.
In the default options struct, we are expecting the following font files:
`FONT_PATH`/Roboto-Regular.ttf `FONT_PATH`/Roboto-Bold.ttf
Creating the document ¶
doc, err := NewInvoice(nil)
By passing nil as the first argument, the default configuration will be used.
Filling the document with the required information ¶
- Issuer
- Customer
- Type
- Number
- Date
- Series
- Mark
- PaymentMethod
- Notes
- QrCodeString
Adding invoice entries ¶
Using the doc.InvoiceDetails.AddEntry method, you can add invoice entries. Those are the items sold or services provided.
Calculate Totals and Taxes ¶
- call doc.CalculateTotals() to calculate the totals
and fill all Taxes entries
doc.Taxes.WithHoldingTaxes = doc.Taxes.Deductions = doc.Taxes.StampDuty = doc.Taxes.Fees = doc.Taxes.OtherTaxes =
Generate the PDF ¶
After providing all the required data we need to create the actual document. Each document has a header, a middle body and a footer. Three corresponding functions are responsible to place on the pdf document the data provided before. We need to provide any struct or data type, that implements the InvoiceCreator interface, which provides those three functions.
We have already one implementation of this interface, the simpleA4Invoice.
We create the document with the following code:
err := doc.CreateAndSave("test.pdf", SimpleA4Invoice{})
Index ¶
- Constants
- type Address
- type Contact
- type Document
- func (doc *Document) AddPage()
- func (doc *Document) AppendBoxToDocument(x, y, w, h, r float64, color []int, styleStr string)
- func (doc *Document) AppendCounterPartToDoc(x, y float64) float64
- func (doc *Document) AppendFootnote(note string)
- func (doc *Document) AppendImage(x, y, w, h float64, imageBytes []byte, name string)
- func (doc *Document) AppendInvoiceDetail(y float64, entry *InvoiceEntry) float64
- func (doc *Document) AppendInvoiceDetailsHeader(y float64) float64
- func (doc *Document) AppendIssuerToDoc(x, y float64) float64
- func (doc *Document) AppendLogoToDocument(x, y float64)
- func (doc *Document) AppendNotes()
- func (doc *Document) AppendQRCode(x, y, w, h float64, qrCodeString string)
- func (doc *Document) AppendTableRow(x float64, y float64, w []float64, h float64, gap float64, color []int, ...) float64
- func (doc *Document) AppendTableRowMultiCell(x float64, y float64, w []float64, h float64, gap float64, color []int, ...) float64
- func (doc *Document) AppendTaxes(endOfDoc bool)
- func (doc *Document) AppendTotals(endOfDoc bool)
- func (doc *Document) CalculateTotals()
- func (doc *Document) Create(creator InvoiceCreator) (*fpdf.Fpdf, error)
- func (doc *Document) CreateAndBuffer(creator InvoiceCreator) (*bytes.Buffer, error)
- func (doc *Document) CreateAndSave(filename string, creator InvoiceCreator) error
- func (doc *Document) CreateLabeledLines(x float64, wLabel float64, wValue float64, height float64, ...)
- func (doc *Document) EncodeString(str string) string
- func (doc *Document) GetAA() string
- func (doc *Document) GetDate() string
- func (doc *Document) GetMark() string
- func (doc *Document) GetPDF() *fpdf.Fpdf
- func (doc *Document) GetSeries() string
- func (doc *Document) GetUID() string
- func (doc *Document) SetCustomer(customer *Contact)
- func (doc *Document) SetDrawColor(color []int)
- func (doc *Document) SetFillColor(color []int)
- func (doc *Document) SetFont(font string, style string, size float64)
- func (doc *Document) SetIssuer(issuer *Contact)
- func (doc *Document) SetTextColor(color []int)
- func (doc *Document) SetXY(x, y float64)
- type InvoiceCreator
- type InvoiceDetails
- type InvoiceEntry
- type Layout
- type Options
- type SimpleA4Invoice
- type TaxEntry
- type UnicodeTranslateFunc
Constants ¶
const ( // BaseMargin define base margin used in documents BaseMargin float64 = 10 // BaseMarginTop define base margin top used in documents BaseMarginTop float64 = 10 )
margins
const ( // BaseTextFontSize define the base font size for text in a document BaseTextFontSize float64 = 12 // MediumTextFontSize define the base font size for text in a document MediumTextFontSize float64 = 10 // SmallTextFontSize define the small font size for text in document SmallTextFontSize float64 = 8 // ExtraSmallTextFontSize define the extra small font size for text in document ExtraSmallTextFontSize float64 = 6 // LargeTextFontSize define the large font size for text in document LargeTextFontSize float64 = 14 )
const ( A4PageWidth = 210.0 A4PageHeight = 297.0 A5PageWidth = 148.0 A5PageHeight = 210.0 A6PageWidth = 105.0 A6PageHeight = 148.0 A7PageWidth = 74.0 A7PageHeight = 105.0 )
const (
LineHeight = 8
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Address ¶
type Address struct { Address string `json:"address,omitempty" validate:"required"` Address2 string `json:"address_2,omitempty"` PostalCode string `json:"postal_code,omitempty"` City string `json:"city,omitempty"` Country string `json:"country,omitempty"` }
Address is a struct that holds the address of a company or a person
func (*Address) ToStringSingleLine ¶
type Contact ¶
type Contact struct { Name string `json:"name" validate:"required,min=1,max=256"` AFM string `json:"afm" validate:"required,min=9,max=9"` WorkDescription string `json:"work_description" validate:"required,min=1,max=256"` Address *Address `json:"address" validate:"required,min=1,max=256"` DOY string `json:"doy" validate:"required,min=1,max=256"` AdditionalInfo []string `json:"additional_info,omitempty"` }
Contact represents contact information.
type Document ¶
type Document struct { Options Options `json:"options,omitempty"` Type string `json:"type,omitempty" validate:"required"` Date string `json:"date,omitempty"` Mark string `json:"mark,omitempty" validate:"required"` Number string `json:"number,omitempty" validate:"required"` Notes string `json:"notes,omitempty" validate:"min_length=1,max_length=256"` Series string `json:"series,omitempty" validate:"required"` PaymentMethod string `json:"paymentMethod,omitempty" validate:"required"` UID string `json:"uid,omit"` QRCodeString string `json:"qr_code_string,omitempty"` InvoiceDetails *InvoiceDetails `json:"invoices_details,omitempty" validate:"required"` Taxes *TaxEntry `json:"taxes,omitempty" validate:"required"` TotalDiscount float64 `json:"total_discount,omitempty" validate:"required"` LogoFileName string `json:"logo_file_name,omitempty"` Issuer *Contact `json:"company,omitempty" validate:"required"` CounterPart *Contact `json:"customer,omitempty"` // contains filtered or unexported fields }
Document represents an invoice document.
func NewInvoice ¶
NewInvoice creates a new invoice document. If the option struct is nil, default options are used.
func (*Document) AddPage ¶
func (doc *Document) AddPage()
AddPage adds a new page to the underlying pdf document
func (*Document) AppendBoxToDocument ¶
AppendBoxToDocument appends a box to the document at position x, y, with width w, and height h. The box is rounded with radius r. The color is specified by the color array [r, g, b] and is the draw color of the box. The styleStr is the style of the box. It can be "D" for dashed, "F" for filled, "DF" for dashed and filled.
func (*Document) AppendCounterPartToDoc ¶
AppendCounterPartToDoc appends the counterpart's contact to the document on the top left of the page.
func (*Document) AppendFootnote ¶
AppendFootnote appends the footer to the document note is the text to be appended to the page number. if the note is equal to, "-", then it includes the mark and the uid of the document.
func (*Document) AppendImage ¶
AppendImage appends an image to the document at position x, y, with width w, and height h. The image is specified by the imageBytes, and the name is the name to store the image internally.
func (*Document) AppendInvoiceDetail ¶
func (doc *Document) AppendInvoiceDetail(y float64, entry *InvoiceEntry) float64
AppendInvoiceDetail appends an invoice detail to the document. This is a row of the table we create
func (*Document) AppendInvoiceDetailsHeader ¶
AppendInvoiceDetailsHeader appends the invoice details to the document. This is the header of the table we create
func (*Document) AppendIssuerToDoc ¶
AppendIssuerToDoc appends the issuer's contact to the document on the specified position. the logo, if provided, is added to the left of the contact.
func (*Document) AppendLogoToDocument ¶
AppendLogoToDocument appends the logo to the document at position x, y. The logo is offset by logoOffsetX, and the height is also defined in options by Layout.LogoHeight.
func (*Document) AppendNotes ¶
func (doc *Document) AppendNotes()
AppendNotes appends the notes to the document. Its box with some "remarks".
func (*Document) AppendQRCode ¶
AppendQRCode appends the QR code to the document at position x, y, with width w, and height h.
func (*Document) AppendTableRow ¶
func (doc *Document) AppendTableRow( x float64, y float64, w []float64, h float64, gap float64, color []int, textColor []int, values []string, fontSize float64, borderStr string) float64
AppendTableRow appends a table row to the document at position x, y, with width w, and height h. The gap is the gap between the cells. The color is specified by the color array [r, g, b] and is the fill color of the row. The textColor is specified by the textColor array [r, g, b] and is the text color of the row. The values are the values of the cells. The fontSize is the font size of the cells. borderStr specifies how the cell border will be drawn. An empty string indicates no border, "1" indicates a full border, and one or more of "L", "T", "R" and "B" indicate the left, top, right and bottom sides of the border.
func (*Document) AppendTableRowMultiCell ¶
func (*Document) AppendTaxes ¶
AppendTaxes appends the taxes section to the document The position is specified in Options.Layout struct by TaxesX and TaxesY. The row height is specified in Options.Layout struct by TaxesRowHeight. The gap is set at 0.5. The fill color is the Options.DarkBgColor, and the text color is the Options.LightTextColor It displays empty values if we are not at the end of the document, on invoices that span multiple pages. This is controlled by endOfDoc.
func (*Document) AppendTotals ¶
AppendTotals appends the totals section to the document Those are the totals of the document and should be displayed at the footer of the document. The position is specified in Options.Layout struct by TotalsX and TotalsY. If endOfDoc is false, the values displayed are -. The labels for the taxes have the Options.AccentColors The values have the Options.DarkTextColor The background color is the Options.LightBgColor
func (*Document) CalculateTotals ¶
func (doc *Document) CalculateTotals()
CalculateTotals calculates the totals of the invoice and appends it to the Taxes struct
func (*Document) Create ¶
func (doc *Document) Create(creator InvoiceCreator) (*fpdf.Fpdf, error)
Create creates a new pdf document using the provided header, middle and footer functions that define the document's content.
func (*Document) CreateAndBuffer ¶
func (doc *Document) CreateAndBuffer(creator InvoiceCreator) (*bytes.Buffer, error)
CreateAndBuffer creates a new pdf document using the provided header, middle and footer functions that define the document's content and returns a buffer with the pdf content.
func (*Document) CreateAndSave ¶
func (doc *Document) CreateAndSave(filename string, creator InvoiceCreator) error
CreateAndSave creates a new pdf document using the provided header, middle and footer functions that define the document's content and saves it to the specified filename.
func (*Document) CreateLabeledLines ¶
func (doc *Document) CreateLabeledLines( x float64, wLabel float64, wValue float64, height float64, fontSizeLabel float64, fontSizeValue float64, colorLabel []int, colorValue []int, bgColor []int, labels []string, values []string, valueAlign string)
CreateLabeledLines creates labeled lines with label: value The function uses the current y position of the document. The x position is specified by the x parameter. The wLabel is the width of the label, and wValue is the width of the value. The height is the height of the line, and the fontSizeLabel and fontSizeValue are the font sizes of the label and value respectively. The colorLabel is the color of the label, and the colorValue is the color of the value.
func (*Document) EncodeString ¶
EncodeString encodes the string using doc.Options.UnicodeTranslateFunc This function can be used if we use a unicode translation function, which I could not make it work with greek encodings. So I just return the string as is.
func (*Document) SetCustomer ¶
SetCustomer set the customer contact
func (*Document) SetDrawColor ¶
SetDrawColor sets the color of the draw
func (*Document) SetFillColor ¶
SetFillColor sets the color of the fill
func (*Document) SetTextColor ¶
SetTextColor sets the color of the text
type InvoiceCreator ¶
type InvoiceDetails ¶
type InvoiceDetails struct {
Entries []*InvoiceEntry `json:"entries,omitempty" validate:"required"`
}
InvoiceDetails represents the details of an invoice
func (*InvoiceDetails) AddEntry ¶
func (inv *InvoiceDetails) AddEntry(entry *InvoiceEntry)
func (*InvoiceDetails) CalculateTotals ¶
func (inv *InvoiceDetails) CalculateTotals() (net, tax, total, discount float64)
type InvoiceEntry ¶
type InvoiceEntry struct { Description string `json:"description,omitempty" validate:"required,min=1,max=41"` Quantity float64 `json:"quantity,omitempty" validate:"required"` UnitOfMeasurement string `json:"unit_of_measurement,omitempty"` NetPrice float64 `json:"net_price,omitempty" validate:"required"` Discount float64 `json:"discount,omitempty"` FinalPrice float64 `json:"final_price,omitempty"` TaxPercent float64 `json:"tax,omitempty"` TaxAmount float64 `json:"tax_amount,omitempty"` }
InvoiceEntry represents an invoice entry. Theses are the data we need to create an invoice entry row
type Layout ¶
type Layout struct { CustomerColumnTwoX float64 `json:"customer_column_two_x,omitempty" validate:"required"` // x position of customer column two CustomerLabelFontSize float64 `json:"customer_label_font_size,omitempty" validate:"required"` // font size of customer label CustomerValueFontSize float64 `json:"customer_value_font_size,omitempty" validate:"required"` // font size of customer value CustomerLabelWidth float64 `json:"customer_label_width,omitempty" validate:"required"` // width of customer label CustomerValueWidth float64 `json:"customer_value_width,omitempty" validate:"required"` // width of customer value CustomerLineHeight float64 `json:"customer_line_height,omitempty" validate:"required"` // height of customer line IssuerPX float64 `json:"issuer_px,omitempty" validate:"required"` // x position of issuer contact IssuerPY float64 `json:"issuer_py,omitempty" validate:"required"` // y position of issuer contact IssuerFontSize float64 `json:"issuer_font_size,omitempty" validate:"required"` // font size of issuer contact InvoiceRowOffset float64 `json:"invoice_row_offset,omitempty" validate:"required"` // offset of invoice row InvoiceColumnGap float64 `json:"invoice_column_gap,omitempty" validate:"required"` // gap between invoice columns LogoOffsetX float64 `json:"logo_offset,omitempty" validate:"required"` // offsetX of logo LogoHeight float64 `json:"logo_height,omitempty" validate:"required"` // height of logo MaxAllowedDetailsHeight float64 `json:"max_allowed_details_height,omitempty" validate:"required"` // max allowed height of details NotesX float64 `json:"notes_x,omitempty" validate:"required"` // x position of notes NotesY float64 `json:"notes_y,omitempty" validate:"required"` // y position of notes NotesWidth float64 `json:"notes_width,omitempty" validate:"required"` // width of notes NotesHeight float64 `json:"notes_height,omitempty" validate:"required"` // height of notes QRCodeX float64 `json:"qrcode_x,omitempty" validate:"required"` // x position of qrcode QRCodeY float64 `json:"qrcode_y,omitempty" validate:"required"` // y position of qrcode QRCodeWidth float64 `json:"qrcode_width,omitempty" validate:"required"` // width of qrcode in mm QRCodeSize float64 `json:"qrcode_size,omitempty" validate:"required"` // size of qrcode in pixels TaxesX float64 `json:"taxes_x,omitempty" validate:"required"` // x position of taxes TaxesY float64 `json:"taxes_y,omitempty" validate:"required"` // y position of taxes TaxesRowHeight float64 `json:"taxes_row_height,omitempty" validate:"required"` // height of taxes row TaxesFontSize float64 `json:"taxes_font_size,omitempty" validate:"required"` // font size of taxes TotalsX float64 `json:"totals_x,omitempty" validate:"required"` // x position of totals TotalsY float64 `json:"totals_y,omitempty" validate:"required"` // y position of totals TotalsFontLabelSize float64 `json:"totals_label_font_size,omitempty" validate:"required"` // font size of totals TotalsFontValueSize float64 `json:"totals_value_font_size,omitempty" validate:"required"` // font size of totals TotalsLabelWidth float64 `json:"totals_label_width,omitempty" validate:"required"` // width of "totals" label TotalsValueWidth float64 `json:"totals_value_width,omitempty" validate:"required"` // width of "totals" value TotalsLineHeight float64 `json:"totals_line_height,omitempty" validate:"required"` // height of totals line }
Layout of the document
type Options ¶
type Options struct { Layout Layout `json:"layout,omitempty"` // layout of the document AutoPrint bool `json:"auto_print,omitempty"` // print without the dialogue box? Orientation string `json:"orientation,omitempty"` // orientation of the document DocumentUnits string `json:"document_units,omitempty"` // units of the document DocumentSize string `json:"document_size,omitempty"` // size of the document CurrencySymbol string `json:"currency_symbol,omitempty"` TextTypeInvoice string `json:"text_type_invoice,omitempty"` TextTypeQuotation string `json:"text_type_quotation,omitempty"` TextTypeDeliveryNote string `json:"text_type_delivery_note,omitempty"` TextRefTitle string `json:"text_ref_title,omitempty"` TextVersionTitle string `json:"text_version_title,omitempty"` TextDateTitle string `json:"text_date_title,omitempty"` TextPaymentTermTitle string `json:"text_payment_term_title,omitempty"` TextItemsNameTitle string `json:"text_items_name_title,omitempty"` TextItemsUnitCostTitle string `json:"text_items_unit_cost_title,omitempty"` TextItemsMeasurementUnit string `json:"text_items_measurement_unit,omitempty"` TextItemsQuantityTitle string `json:"text_items_quantity_title,omitempty"` TextItemsTotalHTTitle string `json:"text_items_total_ht_title,omitempty"` TextItemsTaxTitle string `json:"text_items_tax_title,omitempty"` TextItemsDiscountTitle string `json:"text_items_discount_title,omitempty"` TextItemsTotalTTCTitle string `json:"text_items_total_ttc_title,omitempty"` TextItemNotesTitle string `json:"text_item_notes_title,omitempty"` TextTotalTotal string `json:"text_total_total,omitempty"` TextTotalDiscounted string `json:"text_total_discounted,omitempty"` TextTotalVatTax string `json:"text_total_vat_tax,omitempty"` TextTotalVariousTaxes string `json:"text_total_various_taxes,omitempty"` TextTotalWithHoldingTaxes string `json:"text_total_with_holding_taxes,omitempty"` TextTotalWithTax string `json:"text_total_with_tax,omitempty"` TextWithHoldingTaxes string `json:"text_with_holding_taxes,omitempty"` TextMiscTaxes string `json:"text_misc_taxes,omitempty"` TextDeductions string `json:"text_deductions,omitempty"` TextFeesTaxes string `json:"text_fees_taxes,omitempty"` TextStampTaxes string `json:"text_stamp_taxes,omitempty"` BaseTextColor []int `json:"base_text_color,omitempty"` LightTextColor []int `json:"grey_text_color,omitempty"` LightBgColor []int `json:"grey_bg_color,omitempty"` DarkBgColor []int `json:"dark_bg_color,omitempty"` DarkTextColor []int `json:"dark_text_color,omitempty"` DefaultBgColor []int `json:"default_bg_color,omitempty"` AccentColor []int `json:"accent_color,omitempty"` FontPathEnvName string Font string FontFileName string BoldFont string BoldFontFileName string UnicodeTranslateFunc UnicodeTranslateFunc }
Options for Document
type SimpleA4Invoice ¶
type SimpleA4Invoice struct{}
func (SimpleA4Invoice) InvoiceFooter ¶
func (s SimpleA4Invoice) InvoiceFooter(doc *Document, endOfDoc bool)
func (SimpleA4Invoice) InvoiceHeader ¶
func (s SimpleA4Invoice) InvoiceHeader(doc *Document)
func (SimpleA4Invoice) InvoiceMiddle ¶
func (s SimpleA4Invoice) InvoiceMiddle(doc *Document)
type TaxEntry ¶
type TaxEntry struct { NetAmount float64 `json:"net_amount,omitempty" validate:"required"` TaxAmount float64 `json:"tax_amount,omitempty" validate:"required"` FinalAmount float64 `json:"final_amount,omitempty" validate:"required"` WithHoldingTaxes float64 `json:"with_holding_taxes,omitempty" validate:"required"` Deductions float64 `json:"deductions,omitempty" validate:"required"` StampDuty float64 `json:"stamp_duty,omitempty" validate:"required"` Fees float64 `json:"fees,omitempty" validate:"required"` OtherTaxes float64 `json:"other_taxes,omitempty" validate:"required"` }
TaxEntry represents the taxes of an invoice