mkinvoice/mkinvoice.go

109 lines
2.6 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 logo_top = 6
const logo_height = 23
const line_spacing = 4
const tabstop_left = 20
const tabstop_left_2 = 32
const tabstop_address = 10
const tabstop_count = 10
const tabstop_price = 10
const tabstop_total = 170
const tabstop_logo = 155
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) {
var opt gofpdf.ImageOptions
pdf.AddPage()
y_pos = margin_top
pdf.SetFont("Dejavusans-Bold", "", 9)
WriteText(tabstop_left, y_pos, "nbit Informatik GmbH")
pdf.SetFont("Dejavusans", "", 9)
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "Kirchweg 2")
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "3510 Konolfingen")
y_pos = y_pos + line_spacing
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "Tel.")
WriteText(tabstop_left_2, y_pos, "+41 31 792 00 40")
y_pos = y_pos + line_spacing
WriteText(tabstop_left, y_pos, "EMail")
WriteText(tabstop_left_2, y_pos, "joerg.lehmann@nbit.ch")
y_pos = y_pos + line_spacing
opt.ImageType = "png"
opt.ReadDpi = true
pdf.ImageOptions("logos/nbit-logo.png", tabstop_logo, logo_top, 0, logo_height, false, opt, 0, "")
}
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)
}
}