summaryrefslogtreecommitdiff
path: root/cmd/pdfbook/main.go
blob: 4dc2c934ebbd8f1e9c8b4b8981a600b5e1942445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path"
	"path/filepath"
	"strings"

	"rescribe.xyz/bookpipeline"
)

type Pdfer interface {
	Setup() error
	AddPage(imgpath, hocrpath string) error
	Save(path string) error
}

const pageWidth = 5 // pageWidth in inches

// pxToPt converts a pixel value into a pt value (72 pts per inch)
// This uses pageWidth to determine the appropriate value
func pxToPt(i int) float64 {
	return float64(i) / pageWidth
}

func walker(pdf Pdfer, colour bool) filepath.WalkFunc {
	return func(fpath string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		if !strings.HasSuffix(fpath, ".hocr") {
			return nil
		}
		imgpath := ""
		if colour {
			p := strings.SplitN(path.Base(fpath), "_bin", 2)
			if len(p) > 1 {
				imgpath = path.Join(path.Dir(fpath), p[0] + ".jpg")
			} else {
				imgpath = strings.TrimSuffix(fpath, ".hocr") + ".jpg"
			}
		} else {
			imgpath = strings.TrimSuffix(fpath, ".hocr") + ".png"
		}
		return pdf.AddPage(imgpath, fpath)
	}
}

func main() {
	// TODO: handle best
	// TODO: probably take flags to resize / change quality in due course
	colour := flag.Bool("c", false, "colour")
	flag.Usage = func() {
		fmt.Fprintln(flag.CommandLine.Output(), "Usage: pdfbook [-c] hocrdir out.pdf")
		flag.PrintDefaults()
	}
	flag.Parse()

	if flag.NArg() != 2 {
		flag.Usage()
		return
	}

	pdf := new(bookpipeline.Fpdf)
	pdf.Setup()

	err := filepath.Walk(flag.Arg(0), walker(pdf, *colour))
	if err != nil {
		log.Fatalln("Failed to walk", flag.Arg(0), err)
	}

	err = pdf.Save(flag.Arg(1))
	if err != nil {
		log.Fatalln("Failed to save", flag.Arg(1), err)
	}
}