Compare commits

...

8 Commits

Author SHA1 Message Date
a8e6f98a89 v0.0.501
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
2024-08-07 19:31:36 +02:00
ab805403b9 v0.0.500
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
2024-08-07 19:30:38 +02:00
1e98d351ce v0.0.499
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 5m24s
2024-08-07 18:34:22 +02:00
c40bdc8e9e v0.0.498
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 4m13s
2024-08-07 17:26:35 +02:00
7204562879 v0.0.497
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 2m33s
2024-08-07 17:04:59 +02:00
741611a2e1 v0.0.496 wpdf fixes and wpdf test.go
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 4m58s
2024-08-07 15:34:06 +02:00
133aeb8374 v0.0.495
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m44s
2024-08-07 14:00:02 +02:00
b78a468632 v0.0.494 add tables to wpdf
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
2024-08-07 13:57:29 +02:00
17 changed files with 1120 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.493"
const GoextVersion = "0.0.501"
const GoextVersionTimestamp = "2024-08-07T09:22:37+0200"
const GoextVersionTimestamp = "2024-08-07T19:31:36+0200"

View File

@@ -169,7 +169,7 @@ func EncodeImage(img image.Image, compression ImageCompresson) (bytes.Buffer, st
}
}
func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fillColor color.Color) (image.Image, error) {
func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fillColor color.Color) (image.Image, PercentageRectangle, error) {
iw := img.Bounds().Size().X
ih := img.Bounds().Size().Y
@@ -214,12 +214,12 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{C: fillColor}, image.Pt(0, 0), draw.Src)
draw.Draw(newImg, newImg.Bounds(), img, image.Pt(0, 0), draw.Over)
return newImg, nil
return newImg, PercentageRectangle{0, 0, 1, 1}, nil
}
if fit == ImageFitContainCenter || fit == ImageFitContainTopLeft || fit == ImageFitContainTopRight || fit == ImageFitContainBottomLeft || fit == ImageFitContainBottomRight {
// image-fit:cover fills the target-bounding-box with the image, there is potentially empty-space, it potentially cuts parts of the image away
// image-fit:contain fills the target-bounding-box with the image, there is potentially empty-space, it potentially cuts parts of the image away
// we use the bigger (!) value of facW and facH,
// because the image is made to fit the bounding-box, the bigger factor (= the dimension the image is stretched less) is relevant
@@ -266,7 +266,7 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{C: fillColor}, image.Pt(0, 0), draw.Src)
draw.Draw(newImg, destBounds, img, image.Pt(0, 0), draw.Over)
return newImg, nil
return newImg, calcRelativeRect(destBounds, newImg.Bounds()), nil
}
if fit == ImageFitStretch {
@@ -293,10 +293,10 @@ func ObjectFitImage(img image.Image, bbw float64, bbh float64, fit ImageFit, fil
draw.Draw(newImg, newImg.Bounds(), &image.Uniform{C: fillColor}, image.Pt(0, 0), draw.Src)
draw.Draw(newImg, newImg.Bounds(), img, image.Pt(0, 0), draw.Over)
return newImg, nil
return newImg, PercentageRectangle{0, 0, 1, 1}, nil
}
return nil, exerr.New(exerr.TypeInternal, fmt.Sprintf("unknown image-fit: '%s'", fit)).Build()
return nil, PercentageRectangle{}, exerr.New(exerr.TypeInternal, fmt.Sprintf("unknown image-fit: '%s'", fit)).Build()
}
func VerifyAndDecodeImage(data io.Reader, mime string) (image.Image, error) {

35
imageext/types.go Normal file
View File

@@ -0,0 +1,35 @@
package imageext
import "image"
type Rectangle struct {
X float64
Y float64
W float64
H float64
}
type PercentageRectangle struct {
X float64 // [0..1]
Y float64 // [0..1]
W float64 // [0..1]
H float64 // [0..1]
}
func (r PercentageRectangle) Of(ref Rectangle) Rectangle {
return Rectangle{
X: ref.X + r.X*ref.W,
Y: ref.Y + r.Y*ref.H,
W: r.W * ref.W,
H: r.H * ref.H,
}
}
func calcRelativeRect(inner image.Rectangle, outer image.Rectangle) PercentageRectangle {
return PercentageRectangle{
X: float64(inner.Min.X-outer.Min.X) / float64(outer.Dx()),
Y: float64(inner.Min.Y-outer.Min.Y) / float64(outer.Dy()),
W: float64(inner.Dx()) / float64(outer.Dx()),
H: float64(inner.Dy()) / float64(outer.Dy()),
}
}

View File

@@ -323,6 +323,16 @@ func ArrMap[T1 any, T2 any](arr []T1, conv func(v T1) T2) []T2 {
return r
}
func ArrDeRef[T1 any](arr []*T1) []T1 {
r := make([]T1, 0, len(arr))
for _, v := range arr {
if v != nil {
r = append(r, *v)
}
}
return r
}
func MapMap[TK comparable, TV any, TR any](inmap map[TK]TV, conv func(k TK, v TV) TR) []TR {
r := make([]TR, 0, len(inmap))
for k, v := range inmap {

1
wpdf/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
wpdf_test.pdf

BIN
wpdf/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -3,6 +3,7 @@ package wpdf
import (
"bytes"
"github.com/jung-kurt/gofpdf"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type WPDFBuilder struct {
@@ -13,6 +14,7 @@ type WPDFBuilder struct {
fontName PDFFontFamily
fontStyle PDFFontStyle
fontSize float64
debug bool
}
type PDFMargins struct {
@@ -61,6 +63,19 @@ func (b *WPDFBuilder) SetMargins(v PDFMargins) {
func (b *WPDFBuilder) AddPage() {
b.b.AddPage()
if b.debug {
ml, mt, mr, mb := b.GetMargins()
pw, ph := b.GetPageSize()
b.Rect(pw-ml-mr, ph-mt-mb, RectOutline, NewPDFRectOpt().X(ml).Y(mt).LineWidth(0.25).DrawColor(0, 0, 128))
b.Rect(pw, mt, RectFill, NewPDFRectOpt().X(0).Y(0).FillColor(0, 0, 255).Alpha(0.2, BlendNormal))
b.Rect(ml, ph-mt-mb, RectFill, NewPDFRectOpt().X(0).Y(mt).FillColor(0, 0, 255).Alpha(0.2, BlendNormal))
b.Rect(mr, ph-mt-mb, RectFill, NewPDFRectOpt().X(pw-mr).Y(mt).FillColor(0, 0, 255).Alpha(0.2, BlendNormal))
b.Rect(pw, mb, RectFill, NewPDFRectOpt().X(0).Y(ph-mb).FillColor(0, 0, 255).Alpha(0.2, BlendNormal))
}
}
func (b *WPDFBuilder) SetTextColor(cr, cg, cb int) {
@@ -105,12 +120,38 @@ func (b *WPDFBuilder) SetFont(fontName PDFFontFamily, fontStyle PDFFontStyle, fo
b.cellHeight = b.b.PointConvert(fontSize)
}
func (b *WPDFBuilder) GetFontSize() float64 {
return b.fontSize
}
func (b *WPDFBuilder) GetFontFamily() PDFFontStyle {
return b.fontStyle
}
func (b *WPDFBuilder) GetFontStyle() float64 {
return b.fontSize
}
func (b *WPDFBuilder) SetCellSpacing(h float64) {
b.cellSpacing = h
}
func (b *WPDFBuilder) Ln(h float64) {
xBefore, yBefore := b.GetXY()
b.b.Ln(h)
yAfter := b.GetY()
if b.debug {
_, _, mr, _ := b.GetMargins()
pw, _ := b.GetPageSize()
b.Rect(pw-mr-xBefore, yAfter-yBefore, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(128, 128, 0).Alpha(0.5, BlendNormal))
b.Rect(pw-mr-xBefore, yAfter-yBefore, RectFill, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).FillColor(128, 128, 0).Alpha(0.1, BlendNormal))
b.Line(xBefore, yBefore, pw-mr, yAfter, NewPDFLineOpt().LineWidth(0.25).DrawColor(128, 128, 0))
}
}
func (b *WPDFBuilder) Build() ([]byte, error) {
@@ -192,6 +233,48 @@ func (b *WPDFBuilder) GetWorkAreaWidth() float64 {
return b.GetPageWidth() - b.GetMarginLeft() - b.GetMarginRight()
}
func (b *WPDFBuilder) GetStringWidth(str string) float64 {
func (b *WPDFBuilder) SetAutoPageBreak(auto bool, margin float64) {
b.b.SetAutoPageBreak(auto, margin)
}
func (b *WPDFBuilder) SetFooterFunc(fnc func()) {
b.b.SetFooterFunc(fnc)
}
func (b *WPDFBuilder) PageNo() int {
return b.b.PageNo()
}
func (b *WPDFBuilder) Bookmark(txtStr string, level int, y float64) {
b.b.Bookmark(b.tr(txtStr), level, y)
}
func (b *WPDFBuilder) GetStringWidth(str string, opts ...PDFCellOpt) float64 {
var fontNameOverride *PDFFontFamily
var fontStyleOverride *PDFFontStyle
var fontSizeOverride *float64
for _, opt := range opts {
fontNameOverride = langext.CoalesceOpt(opt.fontNameOverride, fontNameOverride)
fontStyleOverride = langext.CoalesceOpt(opt.fontStyleOverride, fontStyleOverride)
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
}
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
oldFontName := b.fontName
oldFontStyle := b.fontStyle
oldFontSize := b.fontSize
newFontName := langext.Coalesce(fontNameOverride, oldFontName)
newFontStyle := langext.Coalesce(fontStyleOverride, oldFontStyle)
newFontSize := langext.Coalesce(fontSizeOverride, oldFontSize)
b.SetFont(newFontName, newFontStyle, newFontSize)
defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }()
}
return b.b.GetStringWidth(str)
}
func (b *WPDFBuilder) Debug(v bool) {
b.debug = v
}

View File

@@ -1,6 +1,9 @@
package wpdf
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
import (
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type PDFCellOpt struct {
width *float64
@@ -14,6 +17,7 @@ type PDFCellOpt struct {
fontNameOverride *PDFFontFamily
fontStyleOverride *PDFFontStyle
fontSizeOverride *float64
alphaOverride *dataext.Tuple[float64, PDFBlendMode]
extraLn *float64
x *float64
autoWidth *bool
@@ -21,6 +25,7 @@ type PDFCellOpt struct {
borderColor *PDFColor
fillColor *PDFColor
autoWidthPaddingX *float64
debug *bool
}
func NewPDFCellOpt() *PDFCellOpt {
@@ -149,12 +154,45 @@ func (opt *PDFCellOpt) FillColorHex(c uint32) *PDFCellOpt {
return opt
}
func (opt *PDFCellOpt) Alpha(alpha float64, blendMode PDFBlendMode) *PDFCellOpt {
opt.alphaOverride = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return opt
}
func (opt *PDFCellOpt) Debug(v bool) *PDFCellOpt {
opt.debug = &v
return opt
}
func (opt *PDFCellOpt) Copy() *PDFCellOpt {
c := *opt
return &c
}
func (opt *PDFCellOpt) ToMulti() *PDFMultiCellOpt {
return &PDFMultiCellOpt{
width: opt.width,
height: opt.height,
border: opt.border,
align: opt.align,
fill: opt.fill,
fontNameOverride: opt.fontNameOverride,
fontStyleOverride: opt.fontStyleOverride,
fontSizeOverride: opt.fontSizeOverride,
extraLn: opt.extraLn,
x: opt.x,
textColor: opt.textColor,
borderColor: opt.borderColor,
fillColor: opt.fillColor,
}
}
func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
txtTR := b.tr(txt)
width := float64(0)
height := b.cellHeight + b.cellSpacing
var height *float64 = nil
border := BorderNone
ln := BreakToNextLine
align := AlignLeft
@@ -164,6 +202,7 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
var fontNameOverride *PDFFontFamily
var fontStyleOverride *PDFFontStyle
var fontSizeOverride *float64
var alphaOverride *dataext.Tuple[float64, PDFBlendMode]
extraLn := float64(0)
var x *float64
autoWidth := false
@@ -171,10 +210,11 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
var borderColor *PDFColor
var fillColor *PDFColor
autoWidthPaddingX := float64(0)
debug := b.debug
for _, opt := range opts {
width = langext.Coalesce(opt.width, width)
height = langext.Coalesce(opt.height, height)
height = langext.CoalesceOpt(opt.height, height)
border = langext.Coalesce(opt.border, border)
ln = langext.Coalesce(opt.ln, ln)
align = langext.Coalesce(opt.align, align)
@@ -184,6 +224,7 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
fontNameOverride = langext.CoalesceOpt(opt.fontNameOverride, fontNameOverride)
fontStyleOverride = langext.CoalesceOpt(opt.fontStyleOverride, fontStyleOverride)
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
alphaOverride = langext.CoalesceOpt(opt.alphaOverride, alphaOverride)
extraLn = langext.Coalesce(opt.extraLn, extraLn)
x = langext.CoalesceOpt(opt.x, x)
autoWidth = langext.Coalesce(opt.autoWidth, autoWidth)
@@ -191,6 +232,7 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
borderColor = langext.CoalesceOpt(opt.borderColor, borderColor)
fillColor = langext.CoalesceOpt(opt.fillColor, fillColor)
autoWidthPaddingX = langext.Coalesce(opt.autoWidthPaddingX, autoWidthPaddingX)
debug = langext.Coalesce(opt.debug, debug)
}
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
@@ -204,6 +246,11 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
defer func() { b.SetFont(oldFontName, oldFontStyle, oldFontSize) }()
}
if height == nil {
// (do after SetFont, so that b.cellHeight is correctly set to fontOverride)
height = langext.Ptr(b.cellHeight + b.cellSpacing)
}
if textColor != nil {
oldColorR, oldColorG, oldColorB := b.b.GetTextColor()
b.SetTextColor(textColor.R, textColor.G, textColor.B)
@@ -222,15 +269,33 @@ func (b *WPDFBuilder) Cell(txt string, opts ...*PDFCellOpt) {
defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }()
}
if alphaOverride != nil {
oldA, oldBMS := b.b.GetAlpha()
b.b.SetAlpha(alphaOverride.V1, string(alphaOverride.V2))
defer func() { b.b.SetAlpha(oldA, oldBMS) }()
}
if x != nil {
b.b.SetX(*x)
}
if autoWidth {
width = b.b.GetStringWidth(txtTR) + autoWidthPaddingX
width = b.GetStringWidth(txtTR, langext.ArrDeRef(opts)...) + autoWidthPaddingX
}
b.b.CellFormat(width, height, txtTR, string(border), int(ln), string(align), fill, link, linkStr)
xBefore, yBefore := b.b.GetXY()
b.b.CellFormat(width, *height, txtTR, string(border), int(ln), string(align), fill, link, linkStr)
if debug {
if ln == BreakToNextLine {
b.Rect(b.GetPageWidth()-xBefore-b.GetMarginRight(), *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0))
} else if ln == BreakToRight {
b.Rect(b.GetX()-xBefore, *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0))
} else if ln == BreakToBelow {
b.Rect(b.GetPageWidth()-xBefore-b.GetMarginRight(), *height, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0))
}
}
if extraLn != 0 {
b.b.Ln(extraLn)

View File

@@ -74,6 +74,35 @@ const (
RectFillOutline PDFRectStyle = "FD"
)
type PDFBlendMode string
const (
BlendNormal PDFBlendMode = "Normal"
BlendMultiply PDFBlendMode = "Multiply"
BlendScreen PDFBlendMode = "Screen"
BlendOverlay PDFBlendMode = "Overlay"
BlendDarken PDFBlendMode = "Darken"
BlendLighten PDFBlendMode = "Lighten"
BlendColorDodge PDFBlendMode = "ColorDodge"
BlendColorBurn PDFBlendMode = "ColorBurn"
BlendHardLight PDFBlendMode = "HardLight"
BlendSoftLight PDFBlendMode = "SoftLight"
BlendDifference PDFBlendMode = "Difference"
BlendExclusion PDFBlendMode = "Exclusion"
BlendHue PDFBlendMode = "Hue"
BlendSaturation PDFBlendMode = "Saturation"
BlendColor PDFBlendMode = "Color"
BlendLuminosity PDFBlendMode = "Luminosity"
)
type PDFLineCapStyle string
const (
CapButt PDFLineCapStyle = "butt"
CapRound PDFLineCapStyle = "round"
CapSquare PDFLineCapStyle = "square"
)
const (
BackgroundFill = true
BackgroundTransparent = false

View File

@@ -3,6 +3,7 @@ package wpdf
import (
"bytes"
"github.com/jung-kurt/gofpdf"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/imageext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"image"
@@ -130,6 +131,8 @@ type PDFImageOpt struct {
compression *imageext.ImageCompresson
reEncodePixelPerMM *float64
crop *imageext.ImageCrop
alphaOverride *dataext.Tuple[float64, PDFBlendMode]
debug *bool
}
func NewPDFImageOpt() *PDFImageOpt {
@@ -156,6 +159,11 @@ func (opt *PDFImageOpt) Height(v float64) *PDFImageOpt {
return opt
}
func (opt *PDFImageOpt) Debug(v bool) *PDFImageOpt {
opt.debug = &v
return opt
}
func (opt *PDFImageOpt) Flow(v bool) *PDFImageOpt {
opt.flow = &v
return opt
@@ -217,6 +225,11 @@ func (opt *PDFImageOpt) Crop(cropX float64, cropY float64, cropWidth float64, cr
return opt
}
func (opt *PDFImageOpt) Alpha(alpha float64, blendMode PDFBlendMode) *PDFImageOpt {
opt.alphaOverride = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return opt
}
func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
var err error
@@ -234,7 +247,9 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
var imageFit *imageext.ImageFit = nil
var fillColor color.Color = color.Transparent
compression := imageext.CompressionPNGSpeed
debug := b.debug
var crop *imageext.ImageCrop = nil
var alphaOverride *dataext.Tuple[float64, PDFBlendMode]
for _, opt := range opts {
x = langext.Coalesce(opt.x, x)
@@ -252,10 +267,18 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
compression = langext.Coalesce(opt.compression, compression)
reEncodePixelPerMM = langext.Coalesce(opt.reEncodePixelPerMM, reEncodePixelPerMM)
crop = langext.CoalesceOpt(opt.crop, crop)
debug = langext.Coalesce(opt.debug, debug)
alphaOverride = langext.CoalesceOpt(opt.alphaOverride, alphaOverride)
}
if flow {
y = b.GetY()
}
regName := img.Name
var subImageBounds *imageext.PercentageRectangle = nil
if imageFit != nil || fillColor != nil || crop != nil {
var dataimg image.Image
@@ -283,11 +306,14 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
pxw := w * pdfPixelPerMillimeter
pxh := h * pdfPixelPerMillimeter
dataimg, err = imageext.ObjectFitImage(dataimg, pxw, pxh, *imageFit, fillColor)
var dataImgRect imageext.PercentageRectangle
dataimg, dataImgRect, err = imageext.ObjectFitImage(dataimg, pxw, pxh, *imageFit, fillColor)
if err != nil {
b.b.SetError(err)
return
}
subImageBounds = &dataImgRect
}
if dataimg.ColorModel() != color.RGBAModel && dataimg.ColorModel() != color.NRGBAModel {
@@ -318,6 +344,12 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
}
if alphaOverride != nil {
oldA, oldBMS := b.b.GetAlpha()
b.b.SetAlpha(alphaOverride.V1, string(alphaOverride.V2))
defer func() { b.b.SetAlpha(oldA, oldBMS) }()
}
fpdfOpt := gofpdf.ImageOptions{
ImageType: imageType,
ReadDpi: readDpi,
@@ -325,4 +357,16 @@ func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
}
b.b.ImageOptions(regName, x, y, w, h, flow, fpdfOpt, link, linkStr)
if debug {
b.Rect(w, h, RectOutline, NewPDFRectOpt().X(x).Y(y).LineWidth(0.25).DrawColor(255, 0, 0))
if subImageBounds != nil {
r := subImageBounds.Of(imageext.Rectangle{X: x, Y: y, W: w, H: h})
b.Rect(r.W, r.H, RectOutline, NewPDFRectOpt().X(r.X).Y(r.Y).LineWidth(0.25).DrawColor(255, 0, 0))
b.Rect(r.W, r.H, RectFill, NewPDFRectOpt().X(r.X).Y(r.Y).FillColor(255, 0, 0).Alpha(0.2, BlendNormal))
b.Line(r.X, r.Y, r.X+r.W, r.Y+r.H, NewPDFLineOpt().LineWidth(0.25).DrawColor(255, 0, 0))
b.Line(r.X+r.W, r.Y, r.X, r.Y+r.H, NewPDFLineOpt().LineWidth(0.25).DrawColor(255, 0, 0))
}
}
}

96
wpdf/wpdfLine.go Normal file
View File

@@ -0,0 +1,96 @@
package wpdf
import (
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type PDFLineOpt struct {
lineWidth *float64
drawColor *PDFColor
alpha *dataext.Tuple[float64, PDFBlendMode]
capStyle *PDFLineCapStyle
debug *bool
}
func NewPDFLineOpt() *PDFLineOpt {
return &PDFLineOpt{}
}
func (opt *PDFLineOpt) LineWidth(v float64) *PDFLineOpt {
opt.lineWidth = &v
return opt
}
func (opt *PDFLineOpt) DrawColor(cr, cg, cb int) *PDFLineOpt {
opt.drawColor = langext.Ptr(rgbToColor(cr, cg, cb))
return opt
}
func (opt *PDFLineOpt) DrawColorHex(c uint32) *PDFLineOpt {
opt.drawColor = langext.Ptr(hexToColor(c))
return opt
}
func (opt *PDFLineOpt) Alpha(alpha float64, blendMode PDFBlendMode) *PDFLineOpt {
opt.alpha = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return opt
}
func (opt *PDFLineOpt) CapButt() *PDFLineOpt {
opt.capStyle = langext.Ptr(CapButt)
return opt
}
func (opt *PDFLineOpt) CapSquare() *PDFLineOpt {
opt.capStyle = langext.Ptr(CapSquare)
return opt
}
func (opt *PDFLineOpt) CapRound() *PDFLineOpt {
opt.capStyle = langext.Ptr(CapRound)
return opt
}
func (opt *PDFLineOpt) Debug(v bool) *PDFLineOpt {
opt.debug = &v
return opt
}
func (b *WPDFBuilder) Line(x1 float64, y1 float64, x2 float64, y2 float64, opts ...*PDFLineOpt) {
var lineWidth *float64
var drawColor *PDFColor
var alphaOverride *dataext.Tuple[float64, PDFBlendMode]
capStyle := CapButt
debug := b.debug
for _, opt := range opts {
lineWidth = langext.CoalesceOpt(opt.lineWidth, lineWidth)
drawColor = langext.CoalesceOpt(opt.drawColor, drawColor)
alphaOverride = langext.CoalesceOpt(opt.alpha, alphaOverride)
capStyle = langext.Coalesce(opt.capStyle, capStyle)
debug = langext.Coalesce(opt.debug, debug)
}
if lineWidth != nil {
old := b.GetLineWidth()
b.SetLineWidth(*lineWidth)
defer func() { b.SetLineWidth(old) }()
}
if drawColor != nil {
oldR, oldG, oldB := b.GetDrawColor()
b.SetDrawColor(drawColor.R, drawColor.G, drawColor.B)
defer func() { b.SetDrawColor(oldR, oldG, oldB) }()
}
if alphaOverride != nil {
oldA, oldBMS := b.b.GetAlpha()
b.b.SetAlpha(alphaOverride.V1, string(alphaOverride.V2))
defer func() { b.b.SetAlpha(oldA, oldBMS) }()
}
b.b.SetLineCapStyle(string(capStyle))
b.b.Line(x1, y1, x2, y2)
}

View File

@@ -1,6 +1,9 @@
package wpdf
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
import (
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type PDFMultiCellOpt struct {
width *float64
@@ -11,11 +14,13 @@ type PDFMultiCellOpt struct {
fontNameOverride *PDFFontFamily
fontStyleOverride *PDFFontStyle
fontSizeOverride *float64
alphaOverride *dataext.Tuple[float64, PDFBlendMode]
extraLn *float64
x *float64
textColor *PDFColor
borderColor *PDFColor
fillColor *PDFColor
debug *bool
}
func NewPDFMultiCellOpt() *PDFMultiCellOpt {
@@ -119,6 +124,21 @@ func (opt *PDFMultiCellOpt) FillColorHex(c uint32) *PDFMultiCellOpt {
return opt
}
func (opt *PDFMultiCellOpt) Alpha(alpha float64, blendMode PDFBlendMode) *PDFMultiCellOpt {
opt.alphaOverride = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return opt
}
func (opt *PDFMultiCellOpt) Debug(v bool) *PDFMultiCellOpt {
opt.debug = &v
return opt
}
func (opt *PDFMultiCellOpt) Copy() *PDFMultiCellOpt {
c := *opt
return &c
}
func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
txtTR := b.tr(txt)
@@ -131,11 +151,13 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
var fontNameOverride *PDFFontFamily
var fontStyleOverride *PDFFontStyle
var fontSizeOverride *float64
var alphaOverride *dataext.Tuple[float64, PDFBlendMode]
extraLn := float64(0)
var x *float64
var textColor *PDFColor
var borderColor *PDFColor
var fillColor *PDFColor
debug := b.debug
for _, opt := range opts {
width = langext.Coalesce(opt.width, width)
@@ -146,11 +168,13 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
fontNameOverride = langext.CoalesceOpt(opt.fontNameOverride, fontNameOverride)
fontStyleOverride = langext.CoalesceOpt(opt.fontStyleOverride, fontStyleOverride)
fontSizeOverride = langext.CoalesceOpt(opt.fontSizeOverride, fontSizeOverride)
alphaOverride = langext.CoalesceOpt(opt.alphaOverride, alphaOverride)
extraLn = langext.Coalesce(opt.extraLn, extraLn)
x = langext.CoalesceOpt(opt.x, x)
textColor = langext.CoalesceOpt(opt.textColor, textColor)
borderColor = langext.CoalesceOpt(opt.borderColor, borderColor)
fillColor = langext.CoalesceOpt(opt.fillColor, fillColor)
debug = langext.Coalesce(opt.debug, debug)
}
if fontNameOverride != nil || fontStyleOverride != nil || fontSizeOverride != nil {
@@ -182,12 +206,24 @@ func (b *WPDFBuilder) MultiCell(txt string, opts ...*PDFMultiCellOpt) {
defer func() { b.SetFillColor(oldColorR, oldColorG, oldColorB) }()
}
if alphaOverride != nil {
oldA, oldBMS := b.b.GetAlpha()
b.b.SetAlpha(alphaOverride.V1, string(alphaOverride.V2))
defer func() { b.b.SetAlpha(oldA, oldBMS) }()
}
if x != nil {
b.b.SetX(*x)
}
xBefore, yBefore := b.b.GetXY()
b.b.MultiCell(width, height, txtTR, string(border), string(align), fill)
if debug {
b.Rect(b.GetPageWidth()-xBefore-b.GetMarginRight(), b.GetY()-yBefore, RectOutline, NewPDFRectOpt().X(xBefore).Y(yBefore).LineWidth(0.25).DrawColor(0, 128, 0))
}
if extraLn != 0 {
b.b.Ln(extraLn)
}

View File

@@ -1,6 +1,9 @@
package wpdf
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
import (
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type PDFRectOpt struct {
x *float64
@@ -8,10 +11,12 @@ type PDFRectOpt struct {
lineWidth *float64
drawColor *PDFColor
fillColor *PDFColor
alpha *dataext.Tuple[float64, PDFBlendMode]
radiusTL *float64
radiusTR *float64
radiusBR *float64
radiusBL *float64
debug *bool
}
func NewPDFRectOpt() *PDFRectOpt {
@@ -81,16 +86,28 @@ func (opt *PDFRectOpt) RadiusBR(radius float64) *PDFRectOpt {
return opt
}
func (opt *PDFRectOpt) Alpha(alpha float64, blendMode PDFBlendMode) *PDFRectOpt {
opt.alpha = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return opt
}
func (opt *PDFRectOpt) Debug(v bool) *PDFRectOpt {
opt.debug = &v
return opt
}
func (b *WPDFBuilder) Rect(w float64, h float64, styleStr PDFRectStyle, opts ...*PDFRectOpt) {
x := b.GetX()
y := b.GetY()
var lineWidth *float64
var drawColor *PDFColor
var fillColor *PDFColor
var alphaOverride *dataext.Tuple[float64, PDFBlendMode]
radiusTL := float64(0)
radiusTR := float64(0)
radiusBR := float64(0)
radiusBL := float64(0)
debug := b.debug
for _, opt := range opts {
x = langext.Coalesce(opt.x, x)
@@ -98,10 +115,12 @@ func (b *WPDFBuilder) Rect(w float64, h float64, styleStr PDFRectStyle, opts ...
lineWidth = langext.CoalesceOpt(opt.lineWidth, lineWidth)
drawColor = langext.CoalesceOpt(opt.drawColor, drawColor)
fillColor = langext.CoalesceOpt(opt.fillColor, fillColor)
alphaOverride = langext.CoalesceOpt(opt.alpha, alphaOverride)
radiusTL = langext.Coalesce(opt.radiusTL, radiusTL)
radiusTR = langext.Coalesce(opt.radiusTR, radiusTR)
radiusBR = langext.Coalesce(opt.radiusBR, radiusBR)
radiusBL = langext.Coalesce(opt.radiusBL, radiusBL)
debug = langext.Coalesce(opt.debug, debug)
}
if lineWidth != nil {
@@ -122,5 +141,11 @@ func (b *WPDFBuilder) Rect(w float64, h float64, styleStr PDFRectStyle, opts ...
defer func() { b.SetFillColor(oldR, oldG, oldB) }()
}
if alphaOverride != nil {
oldA, oldBMS := b.b.GetAlpha()
b.b.SetAlpha(alphaOverride.V1, string(alphaOverride.V2))
defer func() { b.b.SetAlpha(oldA, oldBMS) }()
}
b.b.RoundedRectExt(x, y, w, h, radiusTL, radiusTR, radiusBR, radiusBL, string(styleStr))
}

337
wpdf/wpdfTable.go Normal file
View File

@@ -0,0 +1,337 @@
package wpdf
import (
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/rext"
"regexp"
"strconv"
)
// Column specifier:
//
// - `{number}`: Use this amount of space
// - `auto`: Use the needed space for the content
// - `*` / `fr`: Use the remaining space, evenly distributed, shrink down to auto
// - `{num}fr` / `{num}*`: Use the remaining space, evenly distributed (weighted), shrink down to auto
//
// # TableBuilder
// - PadX/PadY: Padding between cells
// - DefaultStyle: Default style for cells
//
// # TableCellStyleOpt
// - MultiCell: Use wpdf.MultiCell() instead of wpdf.Cell() --> supports linebreaks
// - Ellipsize: Ellipsize text if too long
// - PaddingHorz: Additional horizontal padding inside of cell to space text around
// - PDFCellOpt: Normal styling options (evtl not all are supported, depending on MultiCell: true/false)
var regexTableColumnSpecFr = rext.W(regexp.MustCompile(`^(?P<num>[0-9]*)(fr|\*)$`))
type TableBuilder struct {
builder *WPDFBuilder
padx float64
pady float64
rows []tableRow
defaultCellStyle *TableCellStyleOpt
columnWidths *[]string
debug *bool
}
func (r tableRow) maxFontSize(defaultFontSize float64) float64 {
mfs := defaultFontSize
for _, cell := range r.cells {
if cell.Style.fontSizeOverride != nil {
mfs = max(mfs, *cell.Style.fontSizeOverride)
}
}
return mfs
}
func (b *TableBuilder) Widths(v ...string) *TableBuilder {
b.columnWidths = &v
return b
}
func (b *TableBuilder) DefaultStyle(s TableCellStyleOpt) *TableBuilder {
b.defaultCellStyle = &s
return b
}
func (b *TableBuilder) PadX(v float64) *TableBuilder {
b.padx = v
return b
}
func (b *TableBuilder) PadY(v float64) *TableBuilder {
b.pady = v
return b
}
func (b *TableBuilder) AddRow(cells ...TableCell) *TableBuilder {
b.rows = append(b.rows, tableRow{cells: cells})
return b
}
func (b *TableBuilder) AddRowWithStyle(style *TableCellStyleOpt, cells ...string) *TableBuilder {
tcels := make([]TableCell, 0, len(cells))
for _, cell := range cells {
tcels = append(tcels, TableCell{Content: cell, Style: *style})
}
b.rows = append(b.rows, tableRow{cells: tcels})
return b
}
func (b *TableBuilder) AddRowDefaultStyle(cells ...string) *TableBuilder {
tcels := make([]TableCell, 0, len(cells))
for _, cell := range cells {
tcels = append(tcels, TableCell{Content: cell, Style: langext.Coalesce(b.defaultCellStyle, TableCellStyleOpt{})})
}
b.rows = append(b.rows, tableRow{cells: tcels})
return b
}
func (b *TableBuilder) BuildRow() *TableRowBuilder {
return &TableRowBuilder{tabbuilder: b, cells: make([]TableCell, 0)}
}
func (b *TableBuilder) Build() {
builder := b.builder
debug := langext.Coalesce(b.debug, b.builder.debug)
if len(b.rows) == 0 {
return // nothing to do
}
_, pageHeight := builder.FPDF().GetPageSize()
pbEnabled, pbMargin := builder.FPDF().GetAutoPageBreak()
builder.FPDF().SetAutoPageBreak(false, 0) // manually handle pagebreak in tables
defer func() { builder.FPDF().SetAutoPageBreak(pbEnabled, pbMargin) }()
columnWidths := b.calculateColumns()
columnCount := len(columnWidths)
for i, dat := range b.rows {
if len(dat.cells) != columnCount {
builder.FPDF().SetError(exerr.New(exerr.TypeInternal, "data must have the same length as header").Int("idx", i).Build())
return
}
}
defaultFontSize, _ := builder.FPDF().GetFontSize()
for rowIdx, row := range b.rows {
nextY := builder.GetY()
for cellIdx, cell := range row.cells {
str := cell.Content
style := cell.Style
ellipsize := langext.Coalesce(style.ellipsize, true)
cellPaddingHorz := langext.Coalesce(style.paddingHorz, 2)
bx := builder.GetX()
by := builder.GetY()
cellWidth := columnWidths[cellIdx]
if langext.Coalesce(style.multiCell, true) {
builder.MultiCell(str, style.PDFCellOpt.Copy().ToMulti().Width(cellWidth).Debug(debug))
} else {
if ellipsize {
if builder.GetStringWidth(str, style.PDFCellOpt) > (cellWidth - cellPaddingHorz) {
for builder.GetStringWidth(str+"...", style.PDFCellOpt) > (cellWidth-cellPaddingHorz) && len(str) > 0 {
str = str[:len(str)-1]
}
str += "..."
}
}
builder.Cell(str, style.PDFCellOpt.Copy().Width(cellWidth).Debug(debug))
}
nextY = max(nextY, builder.GetY())
builder.SetXY(bx+cellWidth+b.padx, by)
}
builder.SetY(nextY + b.pady)
if rowIdx < len(b.rows)-1 && pbEnabled && (builder.GetY()+b.rows[rowIdx+1].maxFontSize(defaultFontSize)) > (pageHeight-pbMargin) {
builder.FPDF().AddPage()
}
}
}
func (b *TableBuilder) calculateColumns() []float64 {
pageWidthTotal, _ := b.builder.FPDF().GetPageSize()
marginLeft, _, marginRight, _ := b.builder.FPDF().GetMargins()
pageWidth := pageWidthTotal - marginLeft - marginRight
columnDef := make([]string, 0)
if b.columnWidths != nil {
columnDef = *b.columnWidths
} else if len(b.rows) > 0 {
columnDef = make([]string, len(b.rows[0].cells))
for i := range columnDef {
columnDef[i] = "*"
}
} else {
return []float64{}
}
columnWidths := make([]float64, len(columnDef))
frColumnWidthCount := 0
frColumnWeights := make([]float64, len(columnDef))
remainingWidth := pageWidth - (float64(len(columnDef)-1) * b.padx)
autoWidths := make([]float64, len(columnDef))
for colIdx := range columnDef {
w := float64(0)
for _, row := range b.rows {
if len(row.cells) > colIdx {
w = max(w, b.builder.GetStringWidth(row.cells[colIdx].Content, row.cells[colIdx].Style.PDFCellOpt))
}
}
autoWidths[colIdx] = w
}
for colIdx, col := range columnDef {
maxPadHorz := float64(0)
minWidth := float64(0)
for _, row := range b.rows {
if len(row.cells) > colIdx {
ph := langext.Coalesce(row.cells[colIdx].Style.paddingHorz, 2)
mw := langext.Coalesce(row.cells[colIdx].Style.minWidth, 0)
minWidth = max(minWidth, ph+mw)
maxPadHorz = max(maxPadHorz, ph)
}
}
if col == "auto" {
w := max(autoWidths[colIdx]+maxPadHorz, minWidth)
columnWidths[colIdx] = w
remainingWidth -= w
} else if match, ok := regexTableColumnSpecFr.MatchFirst(col); ok {
if match.GroupByName("num").Value() == "" {
w := minWidth
frColumnWidthCount += 1
frColumnWeights[colIdx] = 1
columnWidths[colIdx] = w
remainingWidth -= w
} else {
w := minWidth
n, _ := strconv.Atoi(match.GroupByName("num").Value())
frColumnWidthCount += n
frColumnWeights[colIdx] = float64(n)
columnWidths[colIdx] = w
remainingWidth -= w
}
} else {
if w, err := strconv.ParseFloat(col, 64); err == nil {
w = max(w, minWidth)
columnWidths[colIdx] = w
remainingWidth -= w
} else {
b.builder.FPDF().SetError(exerr.New(exerr.TypeInternal, "invalid column width").Str("width", col).Build())
w = max(w, minWidth)
columnWidths[colIdx] = w
remainingWidth -= w
return nil
}
}
}
if remainingWidth < 0 {
// no remaining space to distribute
return columnWidths
}
{
rmSub := 0.0
for i := range columnDef {
if frColumnWeights[i] != 0 {
w := min(autoWidths[i], (remainingWidth/float64(frColumnWidthCount))*frColumnWeights[i])
rmSub += w - columnWidths[i]
columnWidths[i] = w
}
}
remainingWidth -= rmSub
}
if remainingWidth > 0.01 {
rmSub := 0.0
for i, _ := range columnDef {
if frColumnWeights[i] != 0 {
addW := (remainingWidth / float64(frColumnWidthCount)) * frColumnWeights[i]
rmSub += addW
columnWidths[i] += addW
}
}
remainingWidth -= rmSub
}
return columnWidths
}
func (b *TableBuilder) RowCount() int {
return len(b.rows)
}
func (b *TableBuilder) Debug(v bool) *TableBuilder {
b.debug = &v
return b
}
func (b *WPDFBuilder) Table() *TableBuilder {
return &TableBuilder{
builder: b,
rows: make([]tableRow, 0),
pady: 2,
padx: 2,
defaultCellStyle: defaultTableStyle(),
}
}
func defaultTableStyle() *TableCellStyleOpt {
return &TableCellStyleOpt{
PDFCellOpt: *NewPDFCellOpt().
FontSize(float64(8)).
Border(BorderFull).
BorderColorHex(uint32(0x666666)).
FillColorHex(uint32(0xF0F0F0)).
TextColorHex(uint32(0x000000)).
FillBackground(true),
minWidth: langext.Ptr(float64(5)),
ellipsize: langext.PTrue,
multiCell: langext.PFalse,
}
}

187
wpdf/wpdfTableCell.go Normal file
View File

@@ -0,0 +1,187 @@
package wpdf
import (
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
)
type TableCell struct {
Content string
Style TableCellStyleOpt
}
type TableCellStyleOpt struct {
multiCell *bool
ellipsize *bool
paddingHorz *float64
minWidth *float64
fillHeight *bool
PDFCellOpt
}
func NewTableCellStyleOpt() *TableCellStyleOpt {
return &TableCellStyleOpt{}
}
func (o *TableCellStyleOpt) FillHeight(b bool) *TableCellStyleOpt {
o.fillHeight = &b
return o
}
func (o *TableCellStyleOpt) MultiCell(v bool) *TableCellStyleOpt {
o.multiCell = &v
return o
}
func (o *TableCellStyleOpt) Ellipsize(v bool) *TableCellStyleOpt {
o.ellipsize = &v
return o
}
func (o *TableCellStyleOpt) PaddingHorz(v float64) *TableCellStyleOpt {
o.paddingHorz = &v
return o
}
func (o *TableCellStyleOpt) MinWidth(v float64) *TableCellStyleOpt {
o.minWidth = &v
return o
}
func (o *TableCellStyleOpt) CellStyle(v PDFCellOpt) *TableCellStyleOpt {
o.PDFCellOpt = v
return o
}
func (o *TableCellStyleOpt) Width(v float64) *TableCellStyleOpt {
o.PDFCellOpt.width = &v
return o
}
func (o *TableCellStyleOpt) Height(v float64) *TableCellStyleOpt {
o.PDFCellOpt.height = &v
return o
}
func (o *TableCellStyleOpt) Border(v PDFBorder) *TableCellStyleOpt {
o.PDFCellOpt.border = &v
return o
}
func (o *TableCellStyleOpt) LnPos(v PDFTextBreak) *TableCellStyleOpt {
o.PDFCellOpt.ln = &v
return o
}
func (o *TableCellStyleOpt) Align(v PDFTextAlign) *TableCellStyleOpt {
o.PDFCellOpt.align = &v
return o
}
func (o *TableCellStyleOpt) FillBackground(v bool) *TableCellStyleOpt {
o.PDFCellOpt.fill = &v
return o
}
func (o *TableCellStyleOpt) Link(v int) *TableCellStyleOpt {
o.PDFCellOpt.link = &v
return o
}
func (o *TableCellStyleOpt) LinkStr(v string) *TableCellStyleOpt {
o.PDFCellOpt.linkStr = &v
return o
}
func (o *TableCellStyleOpt) Font(fontName PDFFontFamily, fontStyle PDFFontStyle, fontSize float64) *TableCellStyleOpt {
o.PDFCellOpt.fontNameOverride = &fontName
o.PDFCellOpt.fontStyleOverride = &fontStyle
o.PDFCellOpt.fontSizeOverride = &fontSize
return o
}
func (o *TableCellStyleOpt) FontName(v PDFFontFamily) *TableCellStyleOpt {
o.PDFCellOpt.fontNameOverride = &v
return o
}
func (o *TableCellStyleOpt) FontStyle(v PDFFontStyle) *TableCellStyleOpt {
o.PDFCellOpt.fontStyleOverride = &v
return o
}
func (o *TableCellStyleOpt) FontSize(v float64) *TableCellStyleOpt {
o.PDFCellOpt.fontSizeOverride = &v
return o
}
func (o *TableCellStyleOpt) Bold() *TableCellStyleOpt {
o.PDFCellOpt.fontStyleOverride = langext.Ptr(Bold)
return o
}
func (o *TableCellStyleOpt) Italic() *TableCellStyleOpt {
o.PDFCellOpt.fontStyleOverride = langext.Ptr(Italic)
return o
}
func (o *TableCellStyleOpt) LnAfter(v float64) *TableCellStyleOpt {
o.PDFCellOpt.extraLn = &v
return o
}
func (o *TableCellStyleOpt) X(v float64) *TableCellStyleOpt {
o.PDFCellOpt.x = &v
return o
}
func (o *TableCellStyleOpt) AutoWidth() *TableCellStyleOpt {
o.PDFCellOpt.autoWidth = langext.PTrue
return o
}
func (o *TableCellStyleOpt) AutoWidthPaddingX(v float64) *TableCellStyleOpt {
o.PDFCellOpt.autoWidthPaddingX = &v
return o
}
func (o *TableCellStyleOpt) TextColor(cr, cg, cb int) *TableCellStyleOpt {
o.PDFCellOpt.textColor = langext.Ptr(rgbToColor(cr, cg, cb))
return o
}
func (o *TableCellStyleOpt) TextColorHex(c uint32) *TableCellStyleOpt {
o.PDFCellOpt.textColor = langext.Ptr(hexToColor(c))
return o
}
func (o *TableCellStyleOpt) BorderColor(cr, cg, cb int) *TableCellStyleOpt {
o.PDFCellOpt.borderColor = langext.Ptr(rgbToColor(cr, cg, cb))
return o
}
func (o *TableCellStyleOpt) BorderColorHex(c uint32) *TableCellStyleOpt {
o.PDFCellOpt.borderColor = langext.Ptr(hexToColor(c))
return o
}
func (o *TableCellStyleOpt) FillColor(cr, cg, cb int) *TableCellStyleOpt {
o.PDFCellOpt.fillColor = langext.Ptr(rgbToColor(cr, cg, cb))
return o
}
func (o *TableCellStyleOpt) FillColorHex(c uint32) *TableCellStyleOpt {
o.PDFCellOpt.fillColor = langext.Ptr(hexToColor(c))
return o
}
func (o *TableCellStyleOpt) Alpha(alpha float64, blendMode PDFBlendMode) *TableCellStyleOpt {
o.PDFCellOpt.alphaOverride = &dataext.Tuple[float64, PDFBlendMode]{V1: alpha, V2: blendMode}
return o
}
func (o *TableCellStyleOpt) Debug(v bool) *TableCellStyleOpt {
o.PDFCellOpt.debug = &v
return o
}

52
wpdf/wpdfTableRow.go Normal file
View File

@@ -0,0 +1,52 @@
package wpdf
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
type tableRow struct {
cells []TableCell
}
type TableRowBuilder struct {
tabbuilder *TableBuilder
defaultStyle *TableCellStyleOpt
cells []TableCell
}
func (r *TableRowBuilder) RowStyle(style *TableCellStyleOpt) *TableRowBuilder {
r.defaultStyle = style
return r
}
func (r *TableRowBuilder) Cell(cell string) *TableRowBuilder {
r.cells = append(r.cells, TableCell{Content: cell, Style: langext.Coalesce3(r.defaultStyle, r.tabbuilder.defaultCellStyle, TableCellStyleOpt{})})
return r
}
func (r *TableRowBuilder) Cells(cells ...string) *TableRowBuilder {
for _, cell := range cells {
r.cells = append(r.cells, TableCell{Content: cell, Style: langext.Coalesce3(r.defaultStyle, r.tabbuilder.defaultCellStyle, TableCellStyleOpt{})})
}
return r
}
func (r *TableRowBuilder) CellObject(cell TableCell) *TableRowBuilder {
r.cells = append(r.cells, cell)
return r
}
func (r *TableRowBuilder) CellObjects(cells ...TableCell) *TableRowBuilder {
for _, cell := range cells {
r.cells = append(r.cells, cell)
}
return r
}
func (r *TableRowBuilder) CellWithStyle(cell string, style TableCellStyleOpt) *TableRowBuilder {
r.cells = append(r.cells, TableCell{Content: cell, Style: style})
return r
}
func (r *TableRowBuilder) BuildRow() *TableBuilder {
r.tabbuilder.AddRow(r.cells...)
return r.tabbuilder
}

103
wpdf/wpdf_test.go Normal file
View File

@@ -0,0 +1,103 @@
package wpdf
import (
_ "embed"
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/imageext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"os"
"path"
"testing"
)
//go:embed logo.png
var logoData []byte
func TestPDFBuilder(t *testing.T) {
builder := NewPDFBuilder(Portrait, SizeA4, true)
builder.Debug(true)
logoRef := builder.RegisterImage(logoData)
builder.SetMargins(PDFMargins{Left: 15, Top: 40, Right: 10})
builder.AddPage()
builder.SetFont(FontHelvetica, Normal, 10)
builder.Cell("Neueinrichtung deiner Entgeltumwandlung", NewPDFCellOpt().Bold().FontSize(20))
builder.Ln(10)
builder.SetFont(FontHelvetica, Normal, 10)
builder.Cell("Hello World", NewPDFCellOpt().Width(50).Align(AlignHorzCenter).LnPos(BreakToRight))
builder.IncX(10)
builder.Cell("Second Text", NewPDFCellOpt().AutoWidth().AutoWidthPaddingX(2).LnPos(BreakToRight))
builder.Ln(10)
builder.MultiCell("Im Fall einer individuellen Entgeltumwandlung ist die Zuschussverpflichtung auf der Grundlage des Betriebsrentenstärkungsgesetzes in der gesetzlich vorgeschriebenen Höhe (§ 1a Abs. 1a BetrAVG), über den arbeitgeberfinanzierten Zuschuss erfüllt.")
builder.Ln(4)
builder.Image(logoRef, NewPDFImageOpt().X(90).Y(160).Width(70).Height(30).ImageFit(imageext.ImageFitContainCenter))
builder.Ln(4)
cellStyleHeader := &TableCellStyleOpt{
PDFCellOpt: *NewPDFCellOpt().
FontSize(float64(8)).
BorderColorHex(uint32(0x666666)).
Border(BorderFull).
FillColorHex(uint32(0xC0C0C0)).
FillBackground(true).
TextColorHex(uint32(0x000000)).
Align(AlignHorzCenter).
Bold(),
minWidth: langext.Ptr(float64(5)),
ellipsize: langext.PTrue,
multiCell: langext.PFalse,
}
cellStyleMulti := &TableCellStyleOpt{
PDFCellOpt: *NewPDFCellOpt().
FontSize(float64(8)).
BorderColorHex(uint32(0x666666)).
Border(BorderFull).
FillColorHex(uint32(0xC060C0)).
FillBackground(true).
TextColorHex(uint32(0x000000)),
minWidth: langext.Ptr(float64(5)),
ellipsize: langext.PFalse,
multiCell: langext.PTrue,
}
builder.Table().
Widths("auto", "20", "1fr", "20").
PadX(2).
PadY(2).
AddRowWithStyle(cellStyleHeader, "test", "hello", "123", "end").
AddRowDefaultStyle("test", "hello", "123", "end").
AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad").
AddRowDefaultStyle("123asd", "TrimMeTrimMeTrimMeTrimMe", "a", "enwqad").
AddRowWithStyle(cellStyleMulti, "123", "helasdsalo", "a", "MultiCell: enwqad enw\nqad enwqad enwqad enwqad enwqad").
AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad").
Debug(false).
Build()
builder.Table().
Widths("auto", "20", "1fr", "20").
PadX(2).
PadY(2).
BuildRow().RowStyle(cellStyleHeader).Cells("test", "hello", "123", "end").BuildRow().
BuildRow().Cells("test", "hello", "123", "end").BuildRow().
BuildRow().RowStyle(cellStyleMulti.FillHeight(true)).Cell("123").Cell("helasdsalo").Cell("a").Cell("MultiCell: enwqad enw\nqad enwqad enwqad enwqad enwqad").BuildRow().
AddRowDefaultStyle("123", "helasdsalo", "a", "enwqad").
Debug(false).
Build()
bin, err := builder.Build()
if err != nil {
t.Fatal(err)
}
fn := "wpdf_test.pdf"
_ = os.WriteFile(fn, bin, 0644)
fmt.Println("file://" + path.Join(langext.Must(os.Getwd()), fn))
}