Fatura
Visao Geral
O componente Invoice gera um PDF de fatura profissional a partir de dados estruturados. Nenhum codigo de layout manual e necessario — basta fornecer os dados e o gpdf cuida da formatacao.
Uso
import "github.com/gpdf-dev/gpdf/template"
doc := template.Invoice(template.InvoiceData{
Number: "#INV-2026-001",
Date: "March 1, 2026",
DueDate: "March 31, 2026",
From: template.InvoiceParty{
Name: "ACME Corporation",
Address: []string{"123 Business Street", "Suite 100", "San Francisco, CA 94105"},
},
To: template.InvoiceParty{
Name: "John Smith",
Address: []string{"Tech Solutions Inc.", "456 Client Avenue", "New York, NY 10001"},
},
Items: []template.InvoiceItem{
{Description: "Web Development - Frontend", Quantity: "40 hrs", UnitPrice: 150.00, Amount: 6000.00},
{Description: "Web Development - Backend", Quantity: "60 hrs", UnitPrice: 150.00, Amount: 9000.00},
{Description: "UI/UX Design", Quantity: "20 hrs", UnitPrice: 120.00, Amount: 2400.00},
{Description: "Database Design", Quantity: "15 hrs", UnitPrice: 130.00, Amount: 1950.00},
{Description: "QA Testing", Quantity: "25 hrs", UnitPrice: 100.00, Amount: 2500.00},
{Description: "Project Management", Quantity: "10 hrs", UnitPrice: 140.00, Amount: 1400.00},
},
TaxRate: 10,
Currency: "$",
Payment: &template.InvoicePayment{
BankName: "First National Bank",
Account: "1234-5678-9012",
Routing: "021000021",
},
Notes: "Thank you for your business!",
})
data, err := doc.Generate()
┌─ A4 ──────────────────────────────────────────────┐
│ │
│ ACME Corporation INVOICE │
│ 123 Business Street #INV-2026-001 │
│ Suite 100 Date: March 1, 2026 │
│ San Francisco, CA 94105 Due: March 31, 2026 │
│ ──────────────────────────────────────────────── │
│ │
│ Bill To: Payment Info: │
│ John Smith First National Bank │
│ Tech Solutions Inc. Acct: 1234-5678-9012 │
│ 456 Client Avenue Routing: 021000021 │
│ New York, NY 10001 │
│ │
│ ┌──────────────┬────────┬──────────┬──────────┐ │
│ │ Description │ Qty │Unit Price│ Amount │ │
│ ├──────────────┼────────┼──────────┼──────────┤ │
│ │ Frontend │ 40 hrs │ $150.00 │$6,000.00 │ │
│ │ Backend │ 60 hrs │ $150.00 │$9,000.00 │ │
│ │ UI/UX Design │ 20 hrs │ $120.00 │$2,400.00 │ │
│ │ Database │ 15 hrs │ $130.00 │$1,950.00 │ │
│ │ QA Testing │ 25 hrs │ $100.00 │$2,500.00 │ │
│ │ PM │ 10 hrs │ $140.00 │$1,400.00 │ │
│ └──────────────┴────────┴──────────┴──────────┘ │
│ │
│ Subtotal: $23,250.00 │
│ Tax (10%): $2,325.00 │
│ ────────────────── │
│ Total: $25,575.00 │
│ │
│ ──────────────────────────────────────────────── │
│ Thank you for your business! │
│ │
└───────────────────────────────────────────────────┘
Tipos de Dados
InvoiceData
| Campo | Tipo | Descricao |
|---|---|---|
Number | string | Numero da fatura |
Date | string | Data da fatura |
DueDate | string | Data de vencimento |
From | InvoiceParty | Informacoes do remetente |
To | InvoiceParty | Informacoes do destinatario |
Items | []InvoiceItem | Itens da fatura |
TaxRate | float64 | Percentual de imposto (ex: 10 para 10%) |
Currency | string | Simbolo da moeda (ex: "$", "EUR") |
Notes | string | Nota de rodape |
Payment | *InvoicePayment | Informacoes de pagamento (opcional) |
InvoiceParty
type InvoiceParty struct {
Name string
Address []string
}
InvoiceItem
type InvoiceItem struct {
Description string
Quantity string
UnitPrice float64
Amount float64
}
InvoicePayment
type InvoicePayment struct {
BankName string
Account string
Routing string
}
Personalizacao
Passe opcoes de documento para personalizar a fatura:
fontData, _ := os.ReadFile("fonts/NotoSansJP-Regular.ttf")
doc := template.Invoice(invoiceData,
template.WithFont("NotoSansJP", fontData),
template.WithDefaultFont("NotoSansJP", 12),
template.WithPageSize(document.Letter),
)
Usando a Facade
A facade gpdf re-exporta o construtor de fatura:
import "github.com/gpdf-dev/gpdf"
doc := gpdf.NewInvoice(invoiceData)