-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf.go
157 lines (134 loc) · 3.3 KB
/
pdf.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
_ "embed"
"fmt"
"strings"
"time"
"github.com/FurqanSoftware/pog"
"github.com/signintech/gopdf"
)
type PDFBuilder struct {
cfg Config
}
type PDF struct {
Name string
PageCount int
}
func (b PDFBuilder) Build(name string, pr Print) (PDF, error) {
pdf := gopdf.GoPdf{}
pagesize := gopdfPageSizes[b.cfg.Printer.PageSize]
pdf.Start(gopdf.Config{PageSize: *pagesize})
pdf.SetMargins(b.cfg.Printd.MarginLeft, b.cfg.Printd.MarginTop, b.cfg.Printd.MarginRight, b.cfg.Printd.MarginBottom)
pdf.AddPage()
err := pdf.AddTTFFontData("Ubuntu Mono", ubuntuMonoR)
if err != nil {
pog.Fatal(err)
return PDF{}, nil
}
err = pdf.SetFont("Ubuntu Mono", "", b.cfg.Printd.FontSize)
if err != nil {
return PDF{}, err
}
linesperpage := int((pagesize.H - (b.cfg.Printd.MarginTop + b.cfg.Printd.MarginBottom)) / b.cfg.Printd.LineHeight)
header := pr.Header
header += " · " + pr.CreatedAt.In(time.Local).Format(time.DateTime)
headerextra := strings.TrimSpace(b.cfg.Printd.HeaderExtra)
if headerextra != "" {
header += " · " + headerextra
}
var headerlines []string
if header != "" {
headerlines, err = pdf.SplitText(header, pagesize.W-pdf.MarginLeft()-pdf.MarginRight())
if err != nil {
return PDF{}, err
}
}
linesperpage -= len(headerlines) + 2
content := b.tabToSpaces(pr.Content)
var lines []string
if content != "" {
lines, err = pdf.SplitText(content, pagesize.W-pdf.MarginLeft()-pdf.MarginRight())
if err != nil {
return PDF{}, err
}
}
if b.cfg.Printd.ReduceBlankLines {
lines = reduceBlankLines(lines)
}
npages := int((len(lines) + (linesperpage)) / linesperpage)
pageno := 0
for i, l := range lines {
y := pdf.GetY()
if i > 0 {
b.newLine(&pdf)
}
if i == 0 || pdf.GetY() < y {
// New page
pageno++
b.header(&pdf, headerlines, i == 0, pageno, npages)
}
err = pdf.Cell(nil, l)
if err != nil {
return PDF{}, err
}
}
err = pdf.WritePdf(name)
if err != nil {
return PDF{}, err
}
return PDF{
Name: name,
PageCount: npages,
}, err
}
func (b PDFBuilder) header(pdf *gopdf.GoPdf, lines []string, first bool, pageno, npages int) error {
for _, l := range lines {
err := pdf.Cell(nil, l)
if err != nil {
return err
}
b.newLine(pdf)
}
pdf.SetTextColor(92, 104, 115)
var err error
if first {
err = pdf.Cell(nil, fmt.Sprintf("%d/%d · %s", pageno, npages, b.cfg.Toph.BaseURL))
} else {
err = pdf.Cell(nil, fmt.Sprintf("%d/%d", pageno, npages))
}
if err != nil {
return err
}
pdf.SetTextColor(0, 0, 0)
b.newLine(pdf)
b.newLine(pdf)
return nil
}
func (b PDFBuilder) newLine(pdf *gopdf.GoPdf) {
pdf.SetNewXY(pdf.GetY()+float64(b.cfg.Printd.LineHeight), pdf.MarginLeft(), float64(b.cfg.Printd.LineHeight))
}
func (b PDFBuilder) tabToSpaces(t string) string {
return strings.ReplaceAll(t, "\t", strings.Repeat(" ", b.cfg.Printd.TabSize))
}
var (
gopdfPageSizes = map[PageSize]*gopdf.Rect{
PageA4: gopdf.PageSizeA4,
PageLetter: gopdf.PageSizeLetter,
PageLegal: gopdf.PageSizeLegal,
}
)
//go:embed UbuntuMono-R.ttf
var ubuntuMonoR []byte
func reduceBlankLines(lines []string) []string {
reduced := lines[:0]
lastblank := false
for _, l := range lines {
blank := strings.TrimSpace(l) == ""
if lastblank && blank {
continue
}
reduced = append(reduced, l)
lastblank = blank
}
return reduced
}