-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
296 lines (258 loc) · 8.58 KB
/
Copy pathdocument.go
File metadata and controls
296 lines (258 loc) · 8.58 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (c) 2026 the go-pdfkit/pdfkit authors. All rights reserved.
// Use of this source code is governed by a BSD-3-Clause license that can be
// found in the LICENSE file at the root of this repository.
package pdfkit
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"strconv"
"time"
)
// Options configures a Document. The zero value is valid and yields a
// deterministic, uncompressed document with no timestamps.
type Options struct {
// Title and Author populate the document information dictionary. Empty
// values are omitted.
Title string
Author string
// Producer is the /Producer string in the information dictionary. When
// empty it defaults to DefaultProducer.
Producer string
// Now, when non-nil, is called once at Write time to stamp /CreationDate
// and /ModDate. When nil no dates are written, keeping output reproducible;
// tests should leave it nil.
Now func() time.Time
// Compress enables FlateDecode compression of content and embedded-font
// streams. Image streams choose their own filter regardless.
Compress bool
// ID, when both entries are non-nil, is used verbatim as the trailer /ID
// pair. When nil the ID is derived deterministically from the document
// body, so identical documents get identical IDs without a clock.
ID [2][]byte
}
// DefaultProducer is the /Producer value used when Options.Producer is empty.
const DefaultProducer = "go-pdfkit/pdfkit"
// Document is a PDF document under construction. Build it with New, append
// pages with AddPage, then serialise with Write. It is not safe for concurrent
// use.
type Document struct {
opts Options
pages []*Page
fonts []*Font // registration order; index drives the /F<i> name
fontIx map[*Font]int // font -> registration index
use map[*Font]*fontUse // per-document glyph usage, keyed by font
images []*imageXObject // registration order; index drives the /Im<i> name
}
// New returns a new, empty Document configured by opts.
func New(opts Options) *Document {
return &Document{
opts: opts,
fontIx: map[*Font]int{},
use: map[*Font]*fontUse{},
}
}
// AddPage appends a page of the given size (in points; see PageSize helpers and
// the standard sizes such as A4) and returns it for drawing.
func (d *Document) AddPage(size PageSize) *Page {
p := &Page{
doc: d,
width: size.Width,
height: size.Height,
usedFonts: map[*Font]bool{},
usedImages: map[*imageXObject]bool{},
lineWidth: 1,
}
d.pages = append(d.pages, p)
return p
}
// registerFont ensures f has a document resource slot and usage record,
// returning its /F<i> resource name.
func (d *Document) registerFont(f *Font) string {
if _, ok := d.fontIx[f]; !ok {
d.fontIx[f] = len(d.fonts)
d.fonts = append(d.fonts, f)
d.use[f] = newFontUse()
}
return "F" + strconv.Itoa(d.fontIx[f])
}
// registerImage appends an image XObject and returns its /Im<i> resource name.
func (d *Document) registerImage(x *imageXObject) string {
name := "Im" + strconv.Itoa(len(d.images))
d.images = append(d.images, x)
return name
}
// builder assembles the flat list of indirect objects and assigns their
// numbers. Object number n lives at objs[n-1].
type builder struct {
objs []pdfValue
}
// reserve allocates the next object number without a body; fill it with put.
func (bd *builder) reserve() objRef {
bd.objs = append(bd.objs, nil)
return objRef(len(bd.objs))
}
// put stores v as the body of a previously reserved object.
func (bd *builder) put(ref objRef, v pdfValue) { bd.objs[ref-1] = v }
// add reserves and fills an object in one step, returning its reference.
func (bd *builder) add(v pdfValue) objRef {
r := bd.reserve()
bd.put(r, v)
return r
}
// Write serialises the document to w as a complete PDF 1.7 file. It returns the
// first write error encountered. Calling Write does not consume the document;
// it may be written more than once.
func (d *Document) Write(w io.Writer) error {
if len(d.pages) == 0 {
return fmt.Errorf("pdfkit: document has no pages")
}
bd := &builder{}
catalog := bd.reserve()
pagesRef := bd.reserve()
// Reserve a slot per page node up front so the /Kids array can reference
// them before their bodies exist.
pageRefs := make([]objRef, len(d.pages))
for i := range d.pages {
pageRefs[i] = bd.reserve()
}
// All drawing has already happened, so glyph and image usage is final:
// embed the fonts and images first, then reference them from the pages.
fontRefs := make(map[*Font]objRef, len(d.fonts))
for _, f := range d.fonts {
fontRefs[f] = d.buildFont(bd, f)
}
imageRefs := make(map[*imageXObject]objRef, len(d.images))
for _, x := range d.images {
imageRefs[x] = d.buildImage(bd, x)
}
for i, p := range d.pages {
content := p.finishContent()
cdict := newDict()
data := d.maybeFlate(cdict, content)
cstream := bd.add(&pdfStream{dict: cdict, data: data})
node := newDict()
node.set("Type", pdfName("Page"))
node.set("Parent", pagesRef)
node.set("MediaBox", pdfArray{pdfReal(0), pdfReal(0), pdfReal(p.width), pdfReal(p.height)})
node.set("Contents", cstream)
node.set("Resources", p.resources(fontRefs, imageRefs))
bd.put(pageRefs[i], node)
}
kids := make(pdfArray, len(pageRefs))
for i, r := range pageRefs {
kids[i] = r
}
pagesDict := newDict()
pagesDict.set("Type", pdfName("Pages"))
pagesDict.set("Kids", kids)
pagesDict.set("Count", pdfInt(len(pageRefs)))
bd.put(pagesRef, pagesDict)
catDict := newDict()
catDict.set("Type", pdfName("Catalog"))
catDict.set("Pages", pagesRef)
bd.put(catalog, catDict)
var info objRef
hasInfo := d.opts.Title != "" || d.opts.Author != "" || d.producer() != "" || d.opts.Now != nil
if hasInfo {
info = bd.add(d.infoDict())
}
return d.emit(w, bd, catalog, info, hasInfo)
}
// producer returns the effective /Producer string.
func (d *Document) producer() string {
if d.opts.Producer != "" {
return d.opts.Producer
}
return DefaultProducer
}
// infoDict builds the document information dictionary.
func (d *Document) infoDict() *pdfDict {
info := newDict()
if d.opts.Title != "" {
info.set("Title", pdfString(d.opts.Title))
}
if d.opts.Author != "" {
info.set("Author", pdfString(d.opts.Author))
}
if p := d.producer(); p != "" {
info.set("Producer", pdfString(p))
}
if d.opts.Now != nil {
date := pdfString(formatPDFDate(d.opts.Now()))
info.set("CreationDate", date)
info.set("ModDate", date)
}
return info
}
// emit writes the object bodies, the cross-reference table and the trailer.
func (d *Document) emit(w io.Writer, bd *builder, catalog, info objRef, hasInfo bool) error {
var buf bytes.Buffer
buf.WriteString("%PDF-1.7\n")
// A comment with high bytes marks the file as binary for transfer tools.
buf.WriteString("%\xe2\xe3\xcf\xd3\n")
offsets := make([]int, len(bd.objs))
for i, o := range bd.objs {
offsets[i] = buf.Len()
buf.WriteString(strconv.Itoa(i + 1))
buf.WriteString(" 0 obj\n")
o.encodePDF(&buf)
buf.WriteString("\nendobj\n")
}
xrefOff := buf.Len()
n := len(bd.objs) + 1
buf.WriteString("xref\n")
buf.WriteString("0 " + strconv.Itoa(n) + "\n")
buf.WriteString("0000000000 65535 f \n")
for _, off := range offsets {
buf.WriteString(fmt.Sprintf("%010d 00000 n \n", off))
}
trailer := newDict()
trailer.set("Size", pdfInt(n))
trailer.set("Root", catalog)
if hasInfo {
trailer.set("Info", info)
}
id := d.documentID(buf.Bytes())
trailer.set("ID", pdfArray{pdfHexString(id[0]), pdfHexString(id[1])})
buf.WriteString("trailer\n")
trailer.encodePDF(&buf)
buf.WriteString("\nstartxref\n")
buf.WriteString(strconv.Itoa(xrefOff))
buf.WriteString("\n%%EOF\n")
_, err := w.Write(buf.Bytes())
return err
}
// documentID returns the trailer /ID pair. A caller-supplied ID is used as-is;
// otherwise it is derived from the document body so equal documents get equal
// IDs without consulting a clock.
func (d *Document) documentID(body []byte) [2][]byte {
if d.opts.ID[0] != nil && d.opts.ID[1] != nil {
return d.opts.ID
}
sum := sha256.Sum256(body)
h := sum[:16]
return [2][]byte{h, h}
}
// maybeFlate returns data unchanged, or FlateDecode-compressed with the filter
// recorded on dict, according to Options.Compress.
func (d *Document) maybeFlate(dict *pdfDict, data []byte) []byte {
if !d.opts.Compress {
return data
}
dict.set("Filter", pdfName("FlateDecode"))
return flateCompress(data)
}
// formatPDFDate renders t in PDF date syntax, e.g. D:20260728231500+02'00'.
func formatPDFDate(t time.Time) string {
_, off := t.Zone()
sign := '+'
if off < 0 {
sign = '-'
off = -off
}
return fmt.Sprintf("D:%04d%02d%02d%02d%02d%02d%c%02d'%02d'",
t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second(),
sign, off/3600, (off%3600)/60)
}