From 37a1581288447ca63412047fec0fb081043ba6fb Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 30 Aug 2021 13:07:43 +0100 Subject: pdf: Always encode images as jpeg Previously for PDFs using binarised images we kept them as PNG, but there's no good reason to do so, it's better to just get the space savings on offer from jpeg. --- pdf.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pdf.go b/pdf.go index 2287ba9..6543e35 100644 --- a/pdf.go +++ b/pdf.go @@ -12,7 +12,7 @@ import ( "html" "image" "image/jpeg" - "image/png" + _ "image/png" "io/ioutil" "os" @@ -78,7 +78,7 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string, smaller bool) error { if err != nil { return errors.New(fmt.Sprintf("Could not open file %s: %v", imgpath, err)) } - img, imgtype, err := image.Decode(imgf) + img, _, err := image.Decode(imgf) if err != nil { return errors.New(fmt.Sprintf("Could not decode image: %v", err)) } @@ -96,18 +96,14 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string, smaller bool) error { } var buf bytes.Buffer - if imgtype == "jpeg" { - err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality}) - } else { - err = png.Encode(&buf, img) - } + err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality}) if err != nil { return err } p.fpdf.AddPageFormat("P", gofpdf.SizeType{Wd: pxToPt(b.Dx()), Ht: pxToPt(b.Dy())}) - _ = p.fpdf.RegisterImageOptionsReader(imgpath, gofpdf.ImageOptions{ImageType: imgtype}, &buf) + _ = p.fpdf.RegisterImageOptionsReader(imgpath, gofpdf.ImageOptions{ImageType: "jpeg"}, &buf) p.fpdf.ImageOptions(imgpath, 0, 0, pxToPt(b.Dx()), pxToPt(b.Dy()), false, gofpdf.ImageOptions{}, 0, "") p.fpdf.SetTextRenderingMode(3) -- cgit v1.2.1-24-ge1ad