goext/wpdf/wpdfImage.go
Mike Schwörer c28bc086b2
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 3m33s
v0.0.450 wpdf
2024-05-14 11:52:56 +02:00

187 lines
4.1 KiB
Go

package wpdf
import (
"bytes"
"github.com/jung-kurt/gofpdf"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
)
type PDFImageRef struct {
Info *gofpdf.ImageInfoType
Name string
}
type PDFImageRegisterOpt struct {
imageType *string
readDpi *bool
allowNegativePosition *bool
name *string
}
func NewPDFImageRegisterOpt() *PDFImageRegisterOpt {
return &PDFImageRegisterOpt{}
}
func (opt *PDFImageRegisterOpt) ImageType(v string) *PDFImageRegisterOpt {
opt.imageType = &v
return opt
}
func (opt *PDFImageRegisterOpt) ReadDpi(v bool) *PDFImageRegisterOpt {
opt.readDpi = &v
return opt
}
func (opt *PDFImageRegisterOpt) AllowNegativePosition(v bool) *PDFImageRegisterOpt {
opt.allowNegativePosition = &v
return opt
}
func (opt *PDFImageRegisterOpt) Name(v string) *PDFImageRegisterOpt {
opt.name = &v
return opt
}
func (b *WPDFBuilder) RegisterImage(bin []byte, opts ...*PDFImageRegisterOpt) *PDFImageRef {
imgName := "fpdf_img_" + langext.MustRawHexUUID()
imageType := ""
readDpi := false
allowNegativePosition := false
for _, opt := range opts {
imageType = langext.Coalesce(opt.imageType, imageType)
readDpi = langext.Coalesce(opt.readDpi, readDpi)
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
imgName = langext.Coalesce(opt.name, imgName)
}
if imageType == "" {
ct := http.DetectContentType(bin[:512])
switch ct {
case "image/jpg":
imageType = "JPG"
case "image/jpeg":
imageType = "JPEG"
case "image/png":
imageType = "PNG"
case "image/gif":
imageType = "GIF"
}
}
options := gofpdf.ImageOptions{
ImageType: imageType,
ReadDpi: readDpi,
AllowNegativePosition: allowNegativePosition,
}
info := b.b.RegisterImageOptionsReader(imgName, options, bytes.NewReader(bin))
return &PDFImageRef{
Name: imgName,
Info: info,
}
}
type PDFImageOpt struct {
x *float64
y *float64
width *float64
height *float64
flow *bool
link *int
linkStr *string
imageType *string
readDpi *bool
allowNegativePosition *bool
}
func NewPDFImageOpt() *PDFImageOpt {
return &PDFImageOpt{}
}
func (opt *PDFImageOpt) X(v float64) *PDFImageOpt {
opt.x = &v
return opt
}
func (opt *PDFImageOpt) Y(v float64) *PDFImageOpt {
opt.y = &v
return opt
}
func (opt *PDFImageOpt) Width(v float64) *PDFImageOpt {
opt.width = &v
return opt
}
func (opt *PDFImageOpt) Height(v float64) *PDFImageOpt {
opt.height = &v
return opt
}
func (opt *PDFImageOpt) Flow(v bool) *PDFImageOpt {
opt.flow = &v
return opt
}
func (opt *PDFImageOpt) Link(v int) *PDFImageOpt {
opt.link = &v
return opt
}
func (opt *PDFImageOpt) LinkStr(v string) *PDFImageOpt {
opt.linkStr = &v
return opt
}
func (opt *PDFImageOpt) ImageType(v string) *PDFImageOpt {
opt.imageType = &v
return opt
}
func (opt *PDFImageOpt) ReadDpi(v bool) *PDFImageOpt {
opt.readDpi = &v
return opt
}
func (opt *PDFImageOpt) AllowNegativePosition(v bool) *PDFImageOpt {
opt.allowNegativePosition = &v
return opt
}
func (b *WPDFBuilder) Image(img *PDFImageRef, opts ...*PDFImageOpt) {
x := b.GetX()
y := b.GetY()
w := img.Info.Width()
h := img.Info.Height()
flow := true
link := 0
linkStr := ""
imageType := ""
readDpi := false
allowNegativePosition := false
for _, opt := range opts {
x = langext.Coalesce(opt.x, x)
y = langext.Coalesce(opt.y, y)
w = langext.Coalesce(opt.width, w)
h = langext.Coalesce(opt.height, h)
flow = langext.Coalesce(opt.flow, flow)
link = langext.Coalesce(opt.link, link)
linkStr = langext.Coalesce(opt.linkStr, linkStr)
imageType = langext.Coalesce(opt.imageType, imageType)
readDpi = langext.Coalesce(opt.readDpi, readDpi)
allowNegativePosition = langext.Coalesce(opt.allowNegativePosition, allowNegativePosition)
}
fpdfOpt := gofpdf.ImageOptions{
ImageType: imageType,
ReadDpi: readDpi,
AllowNegativePosition: allowNegativePosition,
}
b.b.ImageOptions(img.Name, x, y, w, h, flow, fpdfOpt, link, linkStr)
}