package main
import (
"bytes"
"html/template"
"log"
"os"
)
const invoiceTemplate = "assets/html/invoice.html"
func main() {
// Define the invoice data structure
type Invoice struct {
Host struct {
Name string
Address string
ZipCode string
City string
Phone string
Email string
}
Name string
PhoneNumber string
CustomersNumber int
Platform string
ID string
From string
To string
Total string
AmountPaid string
BalanceDue string
Lines []struct {
Name string
Quantity int
Price string
Total string
}
Payments []struct {
Date string
Method string
Amount string
}
}
// Read the template file
tmpl, err := template.ParseFiles(invoiceTemplate)
if err != nil {
log.Fatalf("Error parsing template: %v", err)
}
// Create sample invoice data
invoice := Invoice{
Host: struct {
Name string
Address string
ZipCode string
City string
Phone string
Email string
}{
Name: "VillaFleurie",
Address: "4 rue Gerty Archimede",
ZipCode: "97190",
City: "Le Gosier",
Phone: "+590 690 44 15 30",
Email: "location.villafleurie@gmail.com",
},
Name: "Michel Le Corre",
//PhoneNumber: "+590 690 44 15 30",
CustomersNumber: 2,
Platform: "Privée",
ID: "VFNI0332",
From: "15 Janvier 2025",
To: "15 Mars 2025",
Total: "2065.00 €",
AmountPaid: "1565.00 €",
BalanceDue: "500.00 €",
Lines: []struct {
Name string
Quantity int
Price string
Total string
}{
{
Name: "T2",
Quantity: 59,
Price: "35.00",
Total: "2065.00",
},
},
Payments: []struct {
Date string
Method string
Amount string
}{
{
"–",
"Espèces",
"500.00",
},
{
"–",
"Chèque",
"1065.00",
},
},
}
// Create a buffer to store the rendered HTML
var buf bytes.Buffer
// Execute the template with the invoice data
if err := tmpl.Execute(&buf, invoice); err != nil {
log.Fatalf("Error executing template: %v", err)
}
// Write the rendered HTML to a file
outputPath := "output.html"
if err := os.WriteFile(outputPath, buf.Bytes(), 0644); err != nil {
log.Fatalf("Error writing HTML file: %v", err)
}
log.Printf("HTML file created successfully: %s", outputPath)
}