goext/wpdf/wpdf.go
Mike Schwörer 59963adf74
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m17s
v0.0.461
2024-05-20 00:52:49 +02:00

198 lines
3.7 KiB
Go

package wpdf
import (
"bytes"
"github.com/jung-kurt/gofpdf"
)
type WPDFBuilder struct {
b *gofpdf.Fpdf
tr func(string) string
cellHeight float64
cellSpacing float64
fontName PDFFontFamily
fontStyle PDFFontStyle
fontSize float64
}
type PDFMargins struct {
Left float64
Top float64
Right float64
}
type PDFColor struct {
R int
G int
B int
}
func NewPDFBuilder(orientation PDFOrientation, size PDFSize, unicode bool) *WPDFBuilder {
fpdfbuilder := gofpdf.New(string(orientation), "mm", string(size), "")
var tr func(string) string
if unicode {
tr = fpdfbuilder.UnicodeTranslatorFromDescriptor("")
} else {
tr = func(s string) string { return s }
}
b := &WPDFBuilder{
b: fpdfbuilder,
tr: tr,
cellHeight: 5,
cellSpacing: 1,
}
b.SetMargins(PDFMargins{Left: 15, Top: 25, Right: 15}) // default values
b.SetFont(FontHelvetica, Normal, 12) // ensures font is set
return b
}
func (b *WPDFBuilder) FPDF() *gofpdf.Fpdf {
return b.b
}
func (b *WPDFBuilder) SetMargins(v PDFMargins) {
b.b.SetMargins(v.Left, v.Top, v.Right)
}
func (b *WPDFBuilder) AddPage() {
b.b.AddPage()
}
func (b *WPDFBuilder) SetTextColor(cr, cg, cb int) {
b.b.SetTextColor(cr, cg, cb)
}
func (b *WPDFBuilder) GetTextColor() (cr, cg, cb int) {
return b.b.GetTextColor()
}
func (b *WPDFBuilder) SetDrawColor(cr, cg, cb int) {
b.b.SetDrawColor(cr, cg, cb)
}
func (b *WPDFBuilder) GetDrawColor() (cr, cg, cb int) {
return b.b.GetDrawColor()
}
func (b *WPDFBuilder) SetFillColor(cr, cg, cb int) {
b.b.SetFillColor(cr, cg, cb)
}
func (b *WPDFBuilder) GetFillColor() (cr, cg, cb int) {
return b.b.GetFillColor()
}
func (b *WPDFBuilder) SetLineWidth(w float64) {
b.b.SetLineWidth(w)
}
func (b *WPDFBuilder) GetLineWidth() float64 {
return b.b.GetLineWidth()
}
func (b *WPDFBuilder) SetFont(fontName PDFFontFamily, fontStyle PDFFontStyle, fontSize float64) {
b.b.SetFont(string(fontName), string(fontStyle), fontSize)
b.fontName = fontName
b.fontStyle = fontStyle
b.fontSize = fontSize
b.cellHeight = b.b.PointConvert(fontSize)
}
func (b *WPDFBuilder) SetCellSpacing(h float64) {
b.cellSpacing = h
}
func (b *WPDFBuilder) Ln(h float64) {
b.b.Ln(h)
}
func (b *WPDFBuilder) Build() ([]byte, error) {
buf := new(bytes.Buffer)
err := b.b.Output(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (b *WPDFBuilder) SetX(x float64) {
b.b.SetX(x)
}
func (b *WPDFBuilder) IncX(dx float64) {
b.b.SetX(b.b.GetX() + dx)
}
func (b *WPDFBuilder) GetX() float64 {
return b.b.GetX()
}
func (b *WPDFBuilder) SetY(y float64) {
b.b.SetY(y)
}
func (b *WPDFBuilder) GetY() float64 {
return b.b.GetY()
}
func (b *WPDFBuilder) SetXY(x float64, y float64) {
b.b.SetXY(x, y)
}
func (b *WPDFBuilder) GetXY() (x float64, y float64) {
return b.b.GetXY()
}
func (b *WPDFBuilder) GetMargins() (left, top, right, bottom float64) {
return b.b.GetMargins()
}
func (b *WPDFBuilder) GetMarginLeft() float64 {
v, _, _, _ := b.b.GetMargins()
return v
}
func (b *WPDFBuilder) GetMarginTop() float64 {
_, v, _, _ := b.b.GetMargins()
return v
}
func (b *WPDFBuilder) GetMarginRight() float64 {
_, _, v, _ := b.b.GetMargins()
return v
}
func (b *WPDFBuilder) GetMarginBottom() float64 {
_, _, _, v := b.b.GetMargins()
return v
}
func (b *WPDFBuilder) GetPageSize() (width, height float64) {
return b.b.GetPageSize()
}
func (b *WPDFBuilder) GetPageWidth() float64 {
v, _ := b.b.GetPageSize()
return v
}
func (b *WPDFBuilder) GetPageHeight() float64 {
_, v := b.b.GetPageSize()
return v
}
func (b *WPDFBuilder) GetWorkAreaWidth() float64 {
return b.GetPageWidth() - b.GetMarginLeft() - b.GetMarginRight()
}
func (b *WPDFBuilder) GetStringWidth(str string) float64 {
return b.b.GetStringWidth(str)
}