-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.go
406 lines (371 loc) · 12.2 KB
/
model.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Implements the in-memory structure of a PDF document, using static types.
// The structure is not exactly the one found or written
// in a PDF, but it serves as an intermediate representation
// to facilitate PDF construction and modification. Still, this library supports
// the majority of the PDF specification.
//
// This package aims at being used without having to think (to much)
// of the PDF implementations details. In particular, unless stated otherwise,
// all the strings should be UTF-8 encoded. The library
// will take care to encode them when needed. They are a few exceptions, where
// ASCII strings are required: it is then up to the user to make sure
// the given string is ASCII.
//
// The entry point of the package is the type `Document`.
package model
import (
"fmt"
"io"
"os"
"time"
)
// Document is the top-level object,
// representing a whole PDF file.
// Where a PDF file use indirect object to
// link data together, `Document` uses Go pointers,
// making easier to analyse and mutate a document.
// See the package `reader` to create a new `Document`
// from an existing PDF file.
// The zero value represents an empty PDF file.
type Document struct {
Trailer Trailer
Catalog Catalog
// // UserPassword, OwnerPassword are not directly part
// // of the PDF document, but are used to protect (encrypt)
// // the contentstream.
// UserPassword, OwnerPassword string
}
// Clone returns a deep copy of the document.
// It may be useful for example when we want to load
// a 'template' document once at server startup, and then
// modifyng it for each request.
// For every type implementing `Referenceable`, the equalities
// between pointers are preserved, meaning that if a pointer is
// used twice in the original document, it will also be used twice in
// the clone (and not duplicated).
func (doc *Document) Clone() Document {
out := *doc
out.Trailer = doc.Trailer.Clone()
out.Catalog = doc.Catalog.Clone()
return out
}
// Write walks the entire document and writes its content
// into `output`, producing a valid PDF file.
// `encryption` is an optional encryption dictionary,
// returned by `UseStandardEncryptionHandler`.
func (doc *Document) Write(output io.Writer, encryption *Encrypt) error {
wr := newWriter(output, encryption)
wr.writeHeader()
doc.Catalog.setupWriter(&wr)
wr.WriteObject(doc.Catalog.pdfString(wr), wr.catalog)
info := wr.CreateObject()
wr.WriteObject(doc.Trailer.Info.pdfString(wr, info), info)
var encRef Reference
if encryption != nil {
encRef = wr.addObject(encryption.pdfString())
}
wr.writeFooter(doc.Trailer, wr.catalog, info, encRef)
return wr.err
}
// WriteFile writes the document in the given file.
// See method `Write` for more information.
func (doc *Document) WriteFile(filename string, encryption *Encrypt) error {
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("can't create PDF file output: %s", err)
}
err = doc.Write(f, encryption)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return fmt.Errorf("can't close PDF file output: %s", err)
}
return nil
}
// Catalog contains the main contents of the document.
// See especially the `Pages` tree, the `AcroForm` form
// and the `Outlines` tree.
type Catalog struct {
Pages PageTree
Extensions Extensions
Names NameDictionary // optional
ViewerPreferences *ViewerPreferences // optional
AcroForm AcroForm // optional
Dests map[Name]DestinationExplicit // optional
PageLabels *PageLabelsTree // optional
Outlines *Outline // optional
StructTreeRoot *StructureTree // optional
MarkInfo *MarkDict // optional
PageLayout Name // optional
PageMode Name // optional
// optional. A simple GoTo action to a direct destination
// may be found as an array in a PDF file.
OpenAction Action
URI string // optional, ASCII string, written in PDF as a dictionary
Lang string
}
func (cat *Catalog) setupWriter(pdf *pdfWriter) {
// register the catalog ref
catalogRef := pdf.CreateObject()
pdf.catalog = catalogRef
// fetch the form field to be merged to annotation
pdf.mergedAccroFields = cat.AcroForm.toBeMerged()
}
// returns the Dictionary of `cat`
// `pdf.catalog` is needed by the potential signature fields
// and destinations
func (cat *Catalog) pdfString(pdf pdfWriter) string {
// some pages may need to know in advance the
// object number of an arbitrary page, such as annotation link
// with GoTo actions
// so, we first walk the tree to associate an object number
// to each pages, so that the second pass can use the map in `pdf`
pdf.allocateReferences(&cat.Pages)
b := newBuffer()
b.line("<<\n/Type/Catalog")
pageRef := pdf.pages[&cat.Pages]
pdf.WriteObject(cat.Pages.pdfString(pdf), pageRef)
b.line("/Pages %s", pageRef)
if pLabel := cat.PageLabels; pLabel != nil {
labelsRef := pdf.CreateObject()
pdf.WriteObject(pLabel.pdfString(pdf, labelsRef, true), labelsRef)
b.line("/PageLabels %s", labelsRef)
}
if names := cat.Names.pdfString(pdf); names != "<<>>" { // avoid writing empty dict
b.line("/Names %s", names)
}
if dests := cat.Dests; len(dests) != 0 {
b.line("/Dests <<")
for name, dest := range dests {
b.line("%s %s", name, dest.pdfDestination(pdf, pdf.catalog))
}
b.line(">>")
}
if viewerPref := cat.ViewerPreferences; viewerPref != nil {
ref := pdf.addObject(viewerPref.pdfString(pdf))
b.line("/ViewerPreferences %s", ref)
}
if p := cat.PageLayout; p != "" {
b.line("/PageLayout %s", p)
}
if p := cat.PageMode; p != "" {
b.line("/PageMode %s", p)
}
if ac := cat.AcroForm; len(ac.Fields) != 0 {
ref := pdf.CreateObject()
pdf.WriteObject(ac.pdfString(pdf, ref), ref)
b.line("/AcroForm %s", ref)
}
if outline := cat.Outlines; outline != nil && outline.First != nil {
outlineRef := pdf.CreateObject()
pdf.WriteObject(outline.pdfString(pdf, outlineRef), outlineRef)
b.line("/Outlines %s", outlineRef)
}
if cat.StructTreeRoot != nil {
stRef := pdf.CreateObject()
pdf.WriteObject(cat.StructTreeRoot.pdfString(pdf, stRef), stRef)
b.line("/StructTreeRoot %s", stRef)
}
if m := cat.MarkInfo; m != nil {
b.line("/MarkInfo %s", m)
}
if cat.URI != "" {
b.line("/URI <</Base %s>>", pdf.EncodeString(cat.URI, ByteString, pdf.catalog))
}
if cat.OpenAction.ActionType != nil {
b.line("/OpenAction %s", cat.OpenAction.pdfString(pdf, pdf.catalog))
}
if cat.Lang != "" {
b.fmt("/Lang " + pdf.EncodeString(cat.Lang, TextString, pdf.catalog))
}
b.fmt(">>")
return b.String()
}
type cloneCache struct {
refs map[Referenceable]Referenceable
pages map[PageNode]PageNode // concrete type are preserved
fields map[*FormFieldDict]*FormFieldDict
structure map[*StructureElement]*StructureElement
// outlines map[*OutlineItem]*OutlineItem
}
func newCloneCache() cloneCache {
return cloneCache{
refs: make(map[Referenceable]Referenceable),
pages: make(map[PageNode]PageNode),
fields: make(map[*FormFieldDict]*FormFieldDict),
structure: make(map[*StructureElement]*StructureElement),
// outlines: make(map[*OutlineItem]*OutlineItem),
}
}
// convenience function to check if the object
// is already cloned and return the clone object, or do the cloning.
// the concrete type of `origin` is preserved, so that the return
// value can be type-asserted back to its original concrete type
func (cache cloneCache) checkOrClone(origin Referenceable) Referenceable {
if cloned := cache.refs[origin]; cloned != nil {
return cloned
}
out := origin.clone(cache)
cache.refs[origin] = out // update the cache
return out
}
// Clone returns a deep copy of the catalog.
func (cat Catalog) Clone() Catalog {
cache := newCloneCache()
// Some pages may need to know in advance the
// pointer to an arbitrary cloned page, such as annotation link
// with GoTo actions
// So, we first walk the tree to allocate new memory
// to each pages, and we do the actual cloning in a second pass
// (at this point, the cache `pages` is filled)
cache.allocateClones(&cat.Pages)
out := cat
outPage := cat.Pages.clone(cache).(*PageTree)
out.Pages = *outPage
out.Names = cat.Names.clone(cache)
if cat.ViewerPreferences != nil {
v := *cat.ViewerPreferences
out.ViewerPreferences = &v
}
out.AcroForm = cat.AcroForm.clone(cache)
if cat.Dests != nil {
out.Dests = make(map[Name]DestinationExplicit, len(cat.Dests))
for k, v := range cat.Dests {
out.Dests[k] = v.clone(cache).(DestinationExplicit)
}
}
if cat.PageLabels != nil {
pl := out.PageLabels.Clone()
cat.PageLabels = &pl
}
out.Outlines = cat.Outlines.clone(cache)
out.StructTreeRoot = cat.StructTreeRoot.clone(cache)
if cat.MarkInfo != nil {
m := *cat.MarkInfo
out.MarkInfo = &m
}
out.OpenAction = cat.OpenAction.clone(cache)
return out
}
// NameDictionary establish the correspondence between names and objects.
// All fields are optional.
// TODO: add more names
type NameDictionary struct {
EmbeddedFiles EmbeddedFileTree
Dests DestTree
AP AppearanceTree
Pages TemplateTree
Templates TemplateTree
}
func (n NameDictionary) pdfString(pdf pdfWriter) string {
b := newBuffer()
b.WriteString("<<")
if dests := n.Dests; !dests.IsEmpty() {
ref := pdf.CreateObject()
pdf.WriteObject(dests.pdfString(pdf, ref), ref)
b.fmt("/Dests %s", ref)
}
if emb := n.EmbeddedFiles; len(emb) != 0 {
ref := pdf.CreateObject()
pdf.WriteObject(emb.pdfString(pdf, ref), ref)
b.fmt("/EmbeddedFiles %s", ref)
}
if aps := n.AP; !aps.IsEmpty() {
ref := pdf.CreateObject()
pdf.WriteObject(aps.pdfString(pdf, ref), ref)
b.fmt("/AP %s", ref)
}
if pages := n.Pages; !pages.IsEmpty() {
ref := pdf.CreateObject()
pdf.WriteObject(pages.pdfString(pdf, ref), ref)
b.fmt("/Pages %s", ref)
}
if templates := n.Templates; !templates.IsEmpty() {
ref := pdf.CreateObject()
pdf.WriteObject(templates.pdfString(pdf, ref), ref)
b.fmt("/Templates %s", ref)
}
b.WriteString(">>")
return b.String()
}
func (n NameDictionary) clone(cache cloneCache) NameDictionary {
out := n
out.EmbeddedFiles = n.EmbeddedFiles.clone(cache)
out.Dests = n.Dests.clone(cache)
out.AP = n.AP.clone(cache)
out.Pages = n.Pages.clone(cache)
out.Templates = n.Templates.clone(cache)
return out
}
// ViewerPreferences specifies the way the document shall be
// displayed on the screen.
// TODO: ViewerPreferences extend the fields
type ViewerPreferences struct {
FitWindow bool
CenterWindow bool
// right to left: determine the relative positioning
// of pages when displayed side by side or printed n-up
DirectionRTL bool
}
func (p ViewerPreferences) pdfString(pdf pdfWriter) string {
direction := Name("L2R")
if p.DirectionRTL {
direction = "R2L"
}
return fmt.Sprintf("<</FitWindow %v /CenterWindow %v /Direction %s>>", p.FitWindow, p.CenterWindow, direction)
}
type Trailer struct {
// TODO: check Prev field
// Encrypt Encrypt
Info Info
ID [2]string // optional (must be not crypted, direct objects)
}
func (t Trailer) Clone() Trailer {
out := t
// out.Encrypt = t.Encrypt.Clone()
return out
}
// Info contains metadata about the document
type Info struct {
Producer string
Title string
Subject string
Author string
Keywords string
Creator string
CreationDate time.Time
ModDate time.Time
}
// pdfString return the Dictionary for `info`
func (info Info) pdfString(pdf pdfWriter, ref Reference) string {
b := newBuffer()
b.fmt("<<\n")
if t := info.Producer; t != "" {
b.fmt("/Producer %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.Title; t != "" {
b.fmt("/Title %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.Subject; t != "" {
b.fmt("/Subject %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.Author; t != "" {
b.fmt("/Author %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.Keywords; t != "" {
b.fmt("/Keywords %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.Creator; t != "" {
b.fmt("/Creator %s\n", pdf.EncodeString(t, TextString, ref))
}
if t := info.CreationDate; !t.IsZero() {
b.fmt("/CreationDate %s\n", pdf.dateString(t, ref))
}
if t := info.ModDate; !t.IsZero() {
b.fmt("/ModDate %s\n", pdf.dateString(t, ref))
}
b.fmt(">>")
return b.String()
}