mkinvoice/mkinvoice.go

97 lines
2.3 KiB
Go

package main
import (
"fmt"
"github.com/jung-kurt/gofpdf"
)
type Adress struct {
lines []string
}
type Item struct {
text string
count float64
price float64
}
type InvoiceData struct {
invoice_number string
invoice_date string
address Adress
items []Item
invoice_total_net float64
invoice_mwst float64
invoice_total float64
}
/* global variable declaration */
var pdf *gofpdf.Fpdf
var y_pos float64
var invoice_data InvoiceData
const margin_top = 7
const line_spacing = 4
const tabstop_left = 20
const tabstop_header_label = 80
const tabstop_header_value = 100
const tabstop_address = 10
const tabstop_count = 10
const tabstop_price = 10
const tabstop_total = 10
func ReadInvoiceData(filename string) {
invoice_data.invoice_number = "1"
invoice_data.invoice_date = "01.01.2021"
invoice_data.address = Adress{ []string{"Jörg Lehmann", "Kirchweg 2", "3510 Konolfingen" }}
invoice_data.items = []Item{ Item{ "Item 1",1,10 }, Item{"Text only",0,0 } }
invoice_data.invoice_total_net = 100
invoice_data.invoice_mwst = 10
invoice_data.invoice_total = 110
}
func WriteText(x float64, y float64, text string, alignStr ...string) {
align := "LT"
if len(alignStr) > 0 {
align = alignStr[0]
}
pdf.SetXY(x, y)
pdf.CellFormat(0, 10, text, "", 0, align, false, 0, "")
}
func SetupInvoice() {
pdf = gofpdf.New("P", "mm", "A4", "")
pdf.SetMargins(0, 0, 0)
pdf.SetFontLocation("fonts")
pdf.AddFont("Dejavusans", "", "DejaVuSans.json")
pdf.AddFont("Dejavusans-Bold", "", "DejaVuSans-Bold.json")
pdf.SetFont("Dejavusans", "", 9)
}
func PrintPageHeader(firstPage bool) {
pdf.AddPage()
y_pos = margin_top
pdf.SetFont("Dejavusans-Bold", "", 9)
WriteText(tabstop_left, y_pos, "nbit Informatik GmbH")
pdf.SetFont("Dejavusans", "", 9)
WriteText(tabstop_header_label, y_pos, "Tel.")
WriteText(tabstop_header_value, y_pos, "+41 31 792 00 40")
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "Kirchweg 2")
WriteText(tabstop_header_label, y_pos, "EMal")
WriteText(tabstop_header_value, y_pos, "joerg.lehmann@nbit.ch")
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "3510 Konolfingen")
}
func main() {
SetupInvoice()
PrintPageHeader(true)
err := pdf.OutputFileAndClose("hello.pdf")
if err == nil {
fmt.Printf("Successfully created invoice\n")
} else {
fmt.Printf("Error: %v\n", err)
}
}