From f1ffc18929106ce5161e65d56c620190d1cf4e90 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 24 Jun 2023 23:53:23 -0400 Subject: [PATCH] fix: scale logo --- pdf.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pdf.go b/pdf.go index 72aec97fc2f46a626cb64f1f1fe2187e62eb21af..93a384ea25ccc0beff72c29bb0c416fb2be93443 100644 --- a/pdf.go +++ b/pdf.go @@ -1,6 +1,9 @@ package main import ( + "fmt" + "image" + "os" "strconv" "github.com/signintech/gopdf" @@ -21,8 +24,11 @@ const ( func writeLogo(pdf *gopdf.GoPdf, logo string, from string) { if logo != "" { - _ = pdf.Image(logo, pdf.GetX(), pdf.GetY(), &gopdf.Rect{W: 44, H: 44}) - pdf.Br(64) + width, height := getImageDimension(logo) + scaledWidth := 100.0 + scaledHeight := float64(height) * scaledWidth / float64(width) + _ = pdf.Image(logo, pdf.GetX(), pdf.GetY(), &gopdf.Rect{W: scaledWidth, H: scaledHeight}) + pdf.Br(scaledHeight + 24) } _ = pdf.SetFont("Inter", "", 12) pdf.SetTextColor(55, 55, 55) @@ -137,3 +143,17 @@ func writeTotal(pdf *gopdf.GoPdf, label string, total float64) { _ = pdf.Cell(nil, currencySymbols[currency]+strconv.FormatFloat(total, 'f', 2, 64)) pdf.Br(24) } + +func getImageDimension(imagePath string) (int, int) { + file, err := os.Open(imagePath) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + } + defer file.Close() + + image, _, err := image.DecodeConfig(file) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err) + } + return image.Width, image.Height +}