summaryrefslogtreecommitdiff
path: root/pdf.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-11-12 16:26:22 +0000
committerNick White <git@njw.name>2019-11-12 16:30:32 +0000
commit3dbd43890928c076fe1a1b8d44cd61ddf8ab72ea (patch)
tree05e2242fe8180a16bee5255f86878eed0a6acb42 /pdf.go
parent1547698dfb3869e48bc79bc53a5e8d2d924a03e8 (diff)
Embed a font, compressed, into the binary
Diffstat (limited to 'pdf.go')
-rw-r--r--pdf.go24
1 files changed, 17 insertions, 7 deletions
diff --git a/pdf.go b/pdf.go
index 4a6e7a1..908f0b6 100644
--- a/pdf.go
+++ b/pdf.go
@@ -1,6 +1,8 @@
package bookpipeline
import (
+ "bytes"
+ "compress/zlib"
"errors"
"fmt"
"html"
@@ -27,14 +29,25 @@ type Fpdf struct {
}
// Setup creates a new PDF with appropriate settings and fonts
-// TODO: find a font that's closer to the average dimensions of the
-// text we're dealing with
-// TODO: once we have a good font, embed it in the binary as bytes
func (p *Fpdf) Setup() error {
p.fpdf = gofpdf.New("P", "pt", "A4", "")
+
// Even though it's invisible, we need to add a font which can do
// UTF-8 so that text renders correctly.
- p.fpdf.AddUTF8Font("dejavu", "", "DejaVuSansCondensed.ttf")
+ // We embed the font directly in the binary, compressed with zlib
+ c := bytes.NewBuffer(dejavucondensed)
+ r, err := zlib.NewReader(c)
+ defer r.Close()
+ if err != nil {
+ return errors.New(fmt.Sprintf("Could not open compressed font", err))
+ }
+ var b bytes.Buffer
+ _, err = b.ReadFrom(r)
+ if err != nil {
+ return errors.New(fmt.Sprintf("Could not read compressed font", err))
+ }
+ p.fpdf.AddUTF8FontFromBytes("dejavu", "", b.Bytes())
+
p.fpdf.SetFont("dejavu", "", 10)
p.fpdf.SetAutoPageBreak(false, float64(0))
return p.fpdf.Error()
@@ -47,7 +60,6 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string) error {
if err != nil {
return errors.New(fmt.Sprintf("Could not read file %s: %v", hocrpath, err))
}
- // TODO: change hocr.Parse to take a Reader rather than []byte
h, err := hocr.Parse(file)
if err != nil {
return errors.New(fmt.Sprintf("Could not parse hocr in file %s: %v", hocrpath, err))
@@ -65,8 +77,6 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string) error {
b := img.Bounds()
p.fpdf.AddPageFormat("P", gofpdf.SizeType{Wd: pxToPt(b.Dx()), Ht: pxToPt(b.Dy())})
- // TODO: check for errors in pdf as going through
-
_ = p.fpdf.RegisterImageOptions(imgpath, gofpdf.ImageOptions{})
p.fpdf.ImageOptions(imgpath, 0, 0, pxToPt(b.Dx()), pxToPt(b.Dy()), false, gofpdf.ImageOptions{}, 0, "")