summaryrefslogtreecommitdiff
path: root/pdf.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-11-20 17:41:35 +0000
committerNick White <git@njw.name>2019-11-20 17:41:35 +0000
commit6a5d6072b1558c43f15393754b7deaa1e3ecad7a (patch)
treec0b4a5ed360ef681b03c30417da4d3d1445f6045 /pdf.go
parent08a305c263329c0d27efd36e3da6d4befcfb4240 (diff)
Implement image resizing option into PDF generation, so that smaller PDFs to be generated
Diffstat (limited to 'pdf.go')
-rw-r--r--pdf.go33
1 files changed, 26 insertions, 7 deletions
diff --git a/pdf.go b/pdf.go
index 9fa52f3..79d2b8c 100644
--- a/pdf.go
+++ b/pdf.go
@@ -7,17 +7,19 @@ import (
"fmt"
"html"
"image"
- _ "image/jpeg"
- _ "image/png"
+ "image/jpeg"
+ "image/png"
"io/ioutil"
"os"
"github.com/jung-kurt/gofpdf"
+ "golang.org/x/image/draw"
"rescribe.xyz/utils/pkg/hocr"
)
// TODO: maybe set this in Fpdf struct
const pageWidth = 5 // pageWidth in inches
+const scaleSmaller = 3 // amount the width and height are divided by
// pxToPt converts a pixel value into a pt value (72 pts per inch)
// This uses pageWidth to determine the appropriate value
@@ -56,7 +58,7 @@ func (p *Fpdf) Setup() error {
// AddPage adds a page to the pdf with an image and (invisible)
// text from an hocr file
-func (p *Fpdf) AddPage(imgpath, hocrpath string) error {
+func (p *Fpdf) AddPage(imgpath, hocrpath string, smaller bool) error {
file, err := ioutil.ReadFile(hocrpath)
if err != nil {
return errors.New(fmt.Sprintf("Could not read file %s: %v", hocrpath, err))
@@ -66,19 +68,36 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string) error {
return errors.New(fmt.Sprintf("Could not parse hocr in file %s: %v", hocrpath, err))
}
- f, err := os.Open(imgpath)
- defer f.Close()
+ imgf, err := os.Open(imgpath)
+ defer imgf.Close()
if err != nil {
return errors.New(fmt.Sprintf("Could not open file %s: %v", imgpath, err))
}
- img, _, err := image.Decode(f)
+ img, imgtype, err := image.Decode(imgf)
if err != nil {
return errors.New(fmt.Sprintf("Could not decode image: %v", err))
}
b := img.Bounds()
+ if smaller {
+ r := image.Rect(0, 0, b.Max.X/scaleSmaller, b.Max.Y/scaleSmaller)
+ smimg := image.NewRGBA(r)
+ draw.ApproxBiLinear.Scale(smimg, r, img, b, draw.Over, nil)
+ img = smimg
+ }
+
+ var buf bytes.Buffer
+ if imgtype == "jpeg" {
+ err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: jpeg.DefaultQuality})
+ } else {
+ err = png.Encode(&buf, img)
+ }
+ if err != nil {
+ return err
+ }
+
p.fpdf.AddPageFormat("P", gofpdf.SizeType{Wd: pxToPt(b.Dx()), Ht: pxToPt(b.Dy())})
- _ = p.fpdf.RegisterImageOptions(imgpath, gofpdf.ImageOptions{})
+ _ = p.fpdf.RegisterImageOptionsReader(imgpath, gofpdf.ImageOptions{ImageType: imgtype}, &buf)
p.fpdf.ImageOptions(imgpath, 0, 0, pxToPt(b.Dx()), pxToPt(b.Dy()), false, gofpdf.ImageOptions{}, 0, "")
p.fpdf.SetTextRenderingMode(3)