-
Notifications
You must be signed in to change notification settings - Fork 44
/
pdf_data.go
626 lines (569 loc) · 14.9 KB
/
pdf_data.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package pdft
import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"io"
"strconv"
"strings"
)
// PDFData pdf file data
type PDFData struct {
trailer TrailerData
xrefs []XrefData
objIDs []int
objs []PDFObjData
pagesObj *PDFObjData
}
// TrailerData trailer
type TrailerData struct {
rootObjID int
}
// Len count
func (p *PDFData) Len() int {
return len(p.objIDs)
}
func (p *PDFData) put(pdfobj PDFObjData) {
p.objIDs = append(p.objIDs, pdfobj.objID)
p.objs = append(p.objs, pdfobj)
}
func (p *PDFData) putNewObject(pdfobj PDFObjData) int {
newObjID := p.maxID() + 1
pdfobj.objID = newObjID
p.put(pdfobj)
return newObjID
}
func (p *PDFData) removeObjByID(objID int) error {
for i, id := range p.objIDs {
if id == objID {
p.objIDs = append(p.objIDs[:i], p.objIDs[i+1:]...)
p.objs = append(p.objs[:i], p.objs[i+1:]...)
return nil
}
}
return errors.New("Not Found")
}
// GetObjByID get obj by objid
func (p *PDFData) getObjByID(objID int) *PDFObjData {
// if pdf exists annotations, it will have multiple same objIDs. So, need find the right one.
indexArr := []int{}
for i, id := range p.objIDs {
if id == objID {
indexArr = append(indexArr, i)
}
}
if len(indexArr) == 1 {
return &p.objs[indexArr[0]]
} else if len(indexArr) > 1 {
result := &p.objs[indexArr[0]]
for _, i := range indexArr {
if props, err := (&p.objs[i]).readProperties(); err == nil && props.getPropByKey("Annots") != nil {
result = &p.objs[i]
}
}
return result
}
return nil
}
// getPageCrawl use crawl, supporting for page nesting
func (p *PDFData) getPageCrawl(objID int, path ...string) (*crawl, error) {
var cw crawl
pagePath := append([]string{"Pages"}, path...)
cw.set(p, objID, pagePath...)
cw.run()
checkedQueue := []int{}
for k := range cw.results {
checkedQueue = append(checkedQueue, k)
}
for len(checkedQueue) > 0 {
key := checkedQueue[0]
if s := cw.results[key].String(); strings.Contains(s, "/Pages") && strings.Contains(s, "/Parent") {
var subCw crawl
subCw.set(p, key, path...)
subCw.run()
for k, v := range subCw.results {
cw.results[k] = v
if _, ok := cw.results[k]; !ok {
checkedQueue = append(checkedQueue, k)
}
}
}
checkedQueue = checkedQueue[1:]
}
return &cw, nil
}
// getPageObjIDs get page obj IDs
func (p *PDFData) getPageObjIDs() ([]int, error) {
results := []int{}
rootProps, _ := p.getObjByID(p.trailer.rootObjID).readProperties()
rootPagesID, _, _ := rootProps.getPropByKey("Pages").asDictionary()
objProps := map[int]*PDFObjPropertiesData{} // cache props
getObjProps := func(id int) *PDFObjPropertiesData {
if v, ok := objProps[id]; ok {
return v
}
if data, err := p.getObjByID(id).readProperties(); err == nil {
objProps[id] = data
return objProps[id]
}
return nil
}
getKids := func(id int) []int {
if props := getObjProps(id); props != nil {
if pages, kid := props.getPropByKey("Pages"), props.getPropByKey("Kids"); pages != nil && kid != nil {
kidIDs, _, _ := kid.asDictionaryArr()
return kidIDs
}
}
return nil
}
isPage := func(id int) bool {
if props := getObjProps(id); props != nil {
return props.getPropByKey("Page") != nil
}
return false
}
var visit func(id int) // Preorder Traversal, supporting for page nesting
visit = func(id int) {
if kids := getKids(id); kids != nil {
for _, kid := range kids {
visit(kid)
}
} else if isPage(id) {
results = append(results, id)
}
}
visit(rootPagesID)
return results, nil
}
func (p *PDFData) maxID() int {
max := 0
for _, id := range p.objIDs {
if id > max {
max = id
}
}
return max
}
func (p *PDFData) injectImgsToPDF(pdfImgs []*PDFImageData) error {
var err error
isEmbedResources := false
rootOfXObjectID := -1
resourcesContent := ""
cwRes, _ := p.getPageCrawl(p.trailer.rootObjID, "Kids", "Resources")
//if err != nil {
// return err
//}
foundRes := false
for resID, r := range cwRes.results {
resources, err := r.valOf("Resources")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
} else {
foundRes = true
resourcesID, _, err := readObjIDFromDictionary(resources)
if err == ErrorObjectIDNotFound {
rootOfXObjectID = resID
resourcesContent = resources
isEmbedResources = true
} else if err != nil {
return err
} else {
rootOfXObjectID = resourcesID
data := p.getObjByID(resourcesID)
if data != nil {
resourcesContent = string(data.data)
}
isEmbedResources = false
}
break
}
}
if !foundRes {
return errors.New("not found /Resources in /Type/Pages")
}
var cw crawl
//cw.set(p, p.trailer.rootObjID, "Pages", "Kids", "Resources", "XObject")
cw.set(p, rootOfXObjectID, "XObject")
err = cw.run()
if err != nil {
return err
}
found := false
xObjectVals := make(map[int]string)
for objID, r := range cw.results {
xobject, err := r.valOf("XObject")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
} else {
xObjectVals[objID] = xobject
found = true
}
}
if !found { //ถ้ายังไม่เจออีก
cw2, _ := p.getPageCrawl(p.trailer.rootObjID, "Kids", "Resources", "XObject")
cw = *cw2
if err != nil {
return err
}
for objID, r := range cw.results {
xobject, err := r.valOf("XObject")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
} else {
xObjectVals[objID] = xobject
found = true
}
}
}
var xobjs crawlResultXObjects
var xObjIndex int
xObjChar := "I"
if found {
for _, xObjectVal := range xObjectVals {
propVal := []byte(xObjectVal)
xobjs.parse(&propVal)
if len(xobjs) > 0 {
xObjChar = xobjs[len(xobjs)-1].xObjChar
if xobjs[len(xobjs)-1].xObjIndex > xObjIndex {
xObjIndex = xobjs[len(xobjs)-1].xObjIndex
}
}
}
}
i := 0
max := len(pdfImgs)
for i < max {
objID := pdfImgs[i].objID
pdfImgs[i].xObjChar = xObjChar
pdfImgs[i].xObjIndex = xObjIndex + i + 1
var xobj crawlResultXObject
xobj.xObjChar = xObjChar
xobj.xObjIndex = xObjIndex + i + 1
xobj.xObjObjID = objID
xobjs = append(xobjs, xobj)
i++
}
objMustReplaces := make(map[int]string)
if found {
for objID, r := range cw.results {
var oldXObjectStr string
oldXObjectStr, err = r.valOf("XObject")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
}
var newXObjs crawlResultXObjects
bOldXObjectStr := []byte(oldXObjectStr)
newXObjs.parse(&bOldXObjectStr)
for _, xobj := range xobjs { // pick new item from xobjs into newXObjs
isExisted := false
for _, existedXObj := range newXObjs {
if existedXObj.xObjChar == xobj.xObjChar && existedXObj.xObjIndex == xobj.xObjIndex { // Avoid conflict of same xObjIndex when editing emerged pdf
isExisted = true
break
}
}
if !isExisted {
newXObjs = append(newXObjs, xobj)
}
}
r.setValOf("XObject", fmt.Sprintf("<<%s>>\n", newXObjs.String()))
objMustReplaces[objID] = r.String()
}
} else {
if isEmbedResources {
var cw01 crawl
cw01.set(p, p.trailer.rootObjID, "Pages", "Kids", "Resources")
err = cw01.run()
if err != nil {
return err
}
for objID, r := range cw01.results {
res, err := r.valOf("Resources")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
} else {
res = strings.TrimSpace(res)
res = fmt.Sprintf("%s /XObject <<%s>>", res[2:len(res)-2], xobjs.String())
r.setValOf("Resources", fmt.Sprintf("<<%s>>\n", res))
objMustReplaces[objID] = r.String()
}
}
} else {
for objID, r := range cw.results {
res := strings.TrimSpace(resourcesContent)
res = fmt.Sprintf("<<%s>>\n", xobjs.String())
r.add("XObject", res)
objMustReplaces[objID] = r.String()
//fmt.Printf("%s\n", r.String())
}
}
}
for objID := range objMustReplaces {
p.getObjByID(objID).data = []byte("<<\n" + objMustReplaces[objID] + ">>\n")
}
return nil
}
func (p *PDFData) injectFontsToPDF(fontDatas map[string]*PDFFontData) error {
var err error
cw, _ := p.getPageCrawl(p.trailer.rootObjID, "Kids", "Resources", "Font")
if err != nil {
return err
}
maxFontIndex, err := findMaxFontIndex(cw, p)
if err != nil {
return err
}
var newCrFonts crawlResultFonts //font ใหม่ที่จะยัดเข้าไป
for _, pdffontdata := range fontDatas {
maxFontIndex++
newCrFonts.append(maxFontIndex, pdffontdata.fontID)
pdffontdata.setFontIndex(maxFontIndex)
}
objMustReplaces := make(map[int]string)
//หา obj ที่ต้องยัด font ใหม่ลงไป
for objID, r := range cw.results { //วน แต่ละ ojb
fontPropVal, err := r.valOf("Font")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return err
}
fontPropValType := propertyType(fontPropVal)
if fontPropValType == object {
var crFonts crawlResultFonts
tmp := []byte(fontPropVal)
err = crFonts.parse(&tmp)
if err != nil {
return err
}
crFonts = append(crFonts, newCrFonts...)
r.setValOf("Font", "<<\n"+crFonts.String()+">>\n")
objMustReplaces[objID] = r.String()
} else if fontPropValType == dictionary {
var fontObjID int
fontObjID, _, err = readObjIDFromDictionary(fontPropVal)
if err != nil {
return err
}
var crFonts crawlResultFonts
fontObj := p.getObjByID(fontObjID)
err = crFonts.parse(&fontObj.data)
if err != nil {
return err
}
crFonts = append(crFonts, newCrFonts...)
objMustReplaces[fontObjID] = crFonts.String()
}
}
for objID := range objMustReplaces {
p.getObjByID(objID).data = []byte("<<\n" + objMustReplaces[objID] + ">>\n")
}
return nil
}
func (p *PDFData) injectContentToPDF(contenters *[]Contenter) error {
var err error
pageBuffs := make(map[int]*bytes.Buffer)
for _, ctn := range *contenters {
pageNum := ctn.page()
if _, ok := pageBuffs[pageNum]; !ok {
pageBuffs[pageNum] = new(bytes.Buffer)
}
var buff *bytes.Buffer
buff, err = ctn.toSteram()
if err != nil {
return err
}
//fmt.Printf("buff=%s\n\n", buff.String())
_, err = buff.WriteTo(pageBuffs[pageNum])
if err != nil {
return err
}
}
pageObjIDs, _ := p.getPageObjIDs()
objMustReplaces := make(map[int]string)
for pageIndex, pageObjID := range pageObjIDs {
var cw2Content crawl
cw2Content.set(p, pageObjID, "Contents")
err = cw2Content.run()
if err != nil {
return err
}
for _, r := range cw2Content.results {
//fmt.Printf("%s\n\n", r.String())
var propContentsVal string
// fmt.Printf("id=%d\n", id)
propContentsVal, err = r.valOf("Contents")
// fmt.Printf("%d propContentsVal=%s\n\n", 0, r.String())
if err == ErrCrawlResultValOfNotFound {
continue
}
propContentsValType := propertyType(propContentsVal)
/*if propContentsValType != dictionary {
return errors.New("not support /Contents type not dictionary yet")
}*/
var contentsObjID int
if propContentsValType == dictionary {
contentsObjID, _, err = readObjIDFromDictionary(propContentsVal)
if err != nil {
return err
}
} else if propContentsValType == array {
contentsObjIDs, _, err := readObjIDFromDictionaryArr(propContentsVal)
if err != nil || len(contentsObjIDs) <= 0 {
return err
}
contentsObjID = contentsObjIDs[0]
} else {
return errors.New("not support /Contents type not dictionary,array yet")
}
data := &p.getObjByID(contentsObjID).data
zip := true
propContentsObj, err := readProperty(data, "FlateDecode")
if err != nil {
return err
}
if propContentsObj == nil {
zip = false
}
var stm *bytes.Buffer
//fmt.Printf("\n-------------------%d-----------------------\n%s\n\n", contentsObjID, string(*data))
stmLen, err := streamLength(p, data)
if err != nil {
return err
}
stm, err = extractStream(data, stmLen, zip)
if err != nil {
return err
}
//fmt.Printf("stm=%s\n\n", stm.String())
if _, ok := pageBuffs[pageIndex+1]; ok {
stm.WriteString("\n")
pageBuffs[pageIndex+1].WriteTo(stm)
objMustReplaces[contentsObjID] = fmt.Sprintf("<<\n/Length %d\n>>\nstream\n%sendstream", stm.Len(), stm.String())
}
}
}
for objID := range objMustReplaces {
//_ = objID
//fmt.Printf("objID=%d\n", objID)
p.getObjByID(objID).data = []byte("" + objMustReplaces[objID] + "")
//fmt.Printf("objId=%d %s\n", objID, string(p.getObjByID(objID).data))
}
return nil
}
func streamLength(p *PDFData, data *[]byte) (int, error) {
prop, err := readProperty(data, "Length")
if err != nil {
return 0, err
}
if prop == nil {
prop, err = readProperty(data, "Length1")
if err != nil {
return 0, err
}
if prop == nil {
return 0, errors.New("/Length or /Length1 not found")
}
}
propType := prop.valType()
if propType == number {
return strconv.Atoi(strings.TrimSpace(prop.rawVal))
} else if propType == dictionary {
objID, _, err := prop.asDictionary()
if err != nil {
return 0, err
}
fontlengthObj := p.getObjByID(objID)
return strconv.Atoi(strings.TrimSpace(string(fontlengthObj.data)))
} else {
return 0, errors.New("/Length or /Length1 wrong type")
}
}
var extractStreamBytes = []byte{0x73, 0x74, 0x72, 0x65, 0x61, 0x6D}
func extractStream(b *[]byte, length int, zip bool) (*bytes.Buffer, error) {
index := bytes.Index(*b, extractStreamBytes)
offset := len(extractStreamBytes)
tmp := (*b)[index+offset:]
tmp = bytes.TrimSpace(tmp)
tmp = tmp[0:length]
var buff bytes.Buffer
buff.Write(tmp)
if !zip {
return &buff, nil
}
r, err := zlib.NewReader(&buff)
if err != nil {
return nil, err
}
defer r.Close()
var out bytes.Buffer
_, err = io.Copy(&out, r)
if err != nil {
return nil, err
}
return &out, nil
}
func findMaxFontIndex(cw *crawl, p *PDFData) (int, error) {
//find max font index
max := 0
for _, item := range cw.results {
fontPropVal, err := item.valOf("Font")
if err == ErrCrawlResultValOfNotFound {
continue
} else if err != nil {
return 0, err
}
var crFonts crawlResultFonts
fontPropValType := propertyType(fontPropVal)
if fontPropValType == object {
tmp := []byte(fontPropVal)
err = crFonts.parse(&tmp)
if err != nil {
return 0, err
}
//fmt.Printf("%#v\n", crFonts)
} else if fontPropValType == dictionary {
var fontObjID int
fontObjID, _, err = readObjIDFromDictionary(fontPropVal)
if err != nil {
return 0, err
}
fontObj := p.getObjByID(fontObjID)
err = crFonts.parse(&fontObj.data)
if err != nil {
return 0, err
}
//fmt.Printf("%#v\n", crFonts)
} else {
return 0, errors.New("not support /Font type array yet")
}
maxFontIndex := crFonts.maxFontIndex()
if maxFontIndex > max {
max = maxFontIndex
}
}
return max, nil
}
func objIDFromStartObjLine(line string) (int, error) {
tokens := strings.Fields(line)
if len(tokens) < 3 {
return 0, errors.New("bad start obj")
}
id, err := strconv.Atoi(strings.TrimSpace(tokens[0]))
if err != nil {
return 0, err
}
return id, nil
}