2016-07-09 14:09:27 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
2016-07-29 17:23:39 +00:00
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
2016-07-09 14:09:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Default writing implementation. Basic output with version 1.3
|
|
|
|
// for compatibility.
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
package model
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-07-14 02:25:29 +00:00
|
|
|
"strings"
|
2016-07-09 14:09:27 +00:00
|
|
|
"time"
|
|
|
|
|
2016-07-17 19:59:17 +00:00
|
|
|
"github.com/unidoc/unidoc/common"
|
2017-07-11 13:08:48 +00:00
|
|
|
"github.com/unidoc/unidoc/common/license"
|
2016-09-08 17:53:45 +00:00
|
|
|
. "github.com/unidoc/unidoc/pdf/core"
|
2018-02-23 14:07:26 +00:00
|
|
|
"github.com/unidoc/unidoc/pdf/model/fonts"
|
2016-07-09 14:09:27 +00:00
|
|
|
)
|
|
|
|
|
2016-08-02 16:31:37 +00:00
|
|
|
var pdfCreator = ""
|
|
|
|
|
|
|
|
func getPdfProducer() string {
|
2017-07-08 22:00:11 +00:00
|
|
|
licenseKey := license.GetLicenseKey()
|
|
|
|
return fmt.Sprintf("UniDoc v%s (%s) - http://unidoc.io", getUniDocVersion(), licenseKey.TypeToString())
|
2016-08-02 16:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPdfCreator() string {
|
|
|
|
if len(pdfCreator) > 0 {
|
|
|
|
return pdfCreator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return default.
|
|
|
|
return "UniDoc - http://unidoc.io"
|
|
|
|
}
|
|
|
|
|
2018-07-15 17:52:53 +00:00
|
|
|
// SetPdfCreator sets the Creator attribute of the output PDF.
|
2016-08-02 16:31:37 +00:00
|
|
|
func SetPdfCreator(creator string) {
|
|
|
|
pdfCreator = creator
|
|
|
|
}
|
|
|
|
|
2018-07-15 17:52:53 +00:00
|
|
|
// PdfWriter handles outputing PDF content.
|
2016-07-09 14:09:27 +00:00
|
|
|
type PdfWriter struct {
|
2016-08-18 09:43:44 +00:00
|
|
|
root *PdfIndirectObject
|
|
|
|
pages *PdfIndirectObject
|
|
|
|
objects []PdfObject
|
|
|
|
objectsMap map[PdfObject]bool // Quick lookup table.
|
|
|
|
writer *bufio.Writer
|
|
|
|
outlines []*PdfIndirectObject
|
|
|
|
outlineTree *PdfOutlineTreeNode
|
|
|
|
catalog *PdfObjectDictionary
|
|
|
|
fields []PdfObject
|
|
|
|
infoObj *PdfIndirectObject
|
2016-12-05 16:21:23 +00:00
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
// Encryption
|
|
|
|
crypter *PdfCrypt
|
|
|
|
encryptDict *PdfObjectDictionary
|
|
|
|
encryptObj *PdfIndirectObject
|
|
|
|
ids *PdfObjectArray
|
2016-12-05 16:21:23 +00:00
|
|
|
|
2017-02-28 08:58:50 +00:00
|
|
|
// PDF version
|
|
|
|
majorVersion int
|
|
|
|
minorVersion int
|
|
|
|
|
2016-12-05 16:21:23 +00:00
|
|
|
// Objects to be followed up on prior to writing.
|
|
|
|
// These are objects that are added and reference objects that are not included
|
|
|
|
// for writing.
|
2016-12-06 01:18:44 +00:00
|
|
|
// The map stores the object and the dictionary it is contained in.
|
|
|
|
// Only way so we can access the dictionary entry later.
|
|
|
|
pendingObjects map[PdfObject]*PdfObjectDictionary
|
2016-12-05 16:21:23 +00:00
|
|
|
|
2016-09-07 17:56:45 +00:00
|
|
|
// Forms.
|
|
|
|
acroForm *PdfAcroForm
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPdfWriter() PdfWriter {
|
|
|
|
w := PdfWriter{}
|
|
|
|
|
|
|
|
w.objectsMap = map[PdfObject]bool{}
|
|
|
|
w.objects = []PdfObject{}
|
2016-12-06 01:18:44 +00:00
|
|
|
w.pendingObjects = map[PdfObject]*PdfObjectDictionary{}
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2017-02-28 08:58:50 +00:00
|
|
|
// PDF Version. Can be changed if using more advanced features in PDF.
|
|
|
|
// By default it is set to 1.3.
|
|
|
|
w.majorVersion = 1
|
|
|
|
w.minorVersion = 3
|
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
// Creation info.
|
2017-07-08 21:04:13 +00:00
|
|
|
infoDict := MakeDict()
|
|
|
|
infoDict.Set("Producer", MakeString(getPdfProducer()))
|
|
|
|
infoDict.Set("Creator", MakeString(getPdfCreator()))
|
2016-07-09 14:09:27 +00:00
|
|
|
infoObj := PdfIndirectObject{}
|
2017-07-08 21:04:13 +00:00
|
|
|
infoObj.PdfObject = infoDict
|
2016-07-09 14:09:27 +00:00
|
|
|
w.infoObj = &infoObj
|
|
|
|
w.addObject(&infoObj)
|
|
|
|
|
|
|
|
// Root catalog.
|
|
|
|
catalog := PdfIndirectObject{}
|
2017-07-08 21:04:13 +00:00
|
|
|
catalogDict := MakeDict()
|
|
|
|
catalogDict.Set("Type", MakeName("Catalog"))
|
|
|
|
catalog.PdfObject = catalogDict
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
w.root = &catalog
|
|
|
|
w.addObject(&catalog)
|
|
|
|
|
|
|
|
// Pages.
|
|
|
|
pages := PdfIndirectObject{}
|
2017-07-08 21:04:13 +00:00
|
|
|
pagedict := MakeDict()
|
|
|
|
pagedict.Set("Type", MakeName("Pages"))
|
2016-07-09 14:09:27 +00:00
|
|
|
kids := PdfObjectArray{}
|
2017-07-08 21:04:13 +00:00
|
|
|
pagedict.Set("Kids", &kids)
|
|
|
|
pagedict.Set("Count", MakeInteger(0))
|
|
|
|
pages.PdfObject = pagedict
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
w.pages = &pages
|
|
|
|
w.addObject(&pages)
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
catalogDict.Set("Pages", &pages)
|
|
|
|
w.catalog = catalogDict
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Catalog %s", catalog)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2017-02-28 08:58:50 +00:00
|
|
|
// Set the PDF version of the output file.
|
|
|
|
func (this *PdfWriter) SetVersion(majorVersion, minorVersion int) {
|
|
|
|
this.majorVersion = majorVersion
|
|
|
|
this.minorVersion = minorVersion
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:21:20 +00:00
|
|
|
// Set the optional content properties.
|
|
|
|
func (this *PdfWriter) SetOCProperties(ocProperties PdfObject) error {
|
|
|
|
dict := this.catalog
|
|
|
|
|
|
|
|
if ocProperties != nil {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Setting OC Properties...")
|
2017-07-08 21:04:13 +00:00
|
|
|
dict.Set("OCProperties", ocProperties)
|
2017-02-15 14:21:20 +00:00
|
|
|
// Any risk of infinite loops?
|
|
|
|
this.addObjects(ocProperties)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
func (this *PdfWriter) hasObject(obj PdfObject) bool {
|
|
|
|
// Check if already added.
|
|
|
|
for _, o := range this.objects {
|
|
|
|
// GH: May perform better to use a hash map to check if added?
|
|
|
|
if o == obj {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the object to list of objects and returns true if the obj was
|
|
|
|
// not already added.
|
|
|
|
// Returns false if the object was previously added.
|
|
|
|
func (this *PdfWriter) addObject(obj PdfObject) bool {
|
|
|
|
hasObj := this.hasObject(obj)
|
|
|
|
if !hasObj {
|
|
|
|
this.objects = append(this.objects, obj)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *PdfWriter) addObjects(obj PdfObject) error {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Adding objects!")
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
if io, isIndirectObj := obj.(*PdfIndirectObject); isIndirectObj {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Indirect")
|
|
|
|
common.Log.Trace("- %s (%p)", obj, io)
|
|
|
|
common.Log.Trace("- %s", io.PdfObject)
|
2016-07-09 14:09:27 +00:00
|
|
|
if this.addObject(io) {
|
|
|
|
err := this.addObjects(io.PdfObject)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if so, isStreamObj := obj.(*PdfObjectStream); isStreamObj {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Stream")
|
|
|
|
common.Log.Trace("- %s %p", obj, obj)
|
2016-07-09 14:09:27 +00:00
|
|
|
if this.addObject(so) {
|
|
|
|
err := this.addObjects(so.PdfObjectDictionary)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if dict, isDict := obj.(*PdfObjectDictionary); isDict {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Dict")
|
|
|
|
common.Log.Trace("- %s", obj)
|
2017-07-08 21:04:13 +00:00
|
|
|
for _, k := range dict.Keys() {
|
|
|
|
v := dict.Get(k)
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Key %s", k)
|
2016-07-09 14:09:27 +00:00
|
|
|
if k != "Parent" {
|
|
|
|
err := this.addObjects(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-16 16:22:01 +00:00
|
|
|
} else {
|
2017-07-08 21:04:13 +00:00
|
|
|
if _, parentIsNull := dict.Get("Parent").(*PdfObjectNull); parentIsNull {
|
2016-12-05 16:21:23 +00:00
|
|
|
// Parent is null. We can ignore it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:53:45 +00:00
|
|
|
if hasObj := this.hasObject(v); !hasObj {
|
2017-04-19 12:05:20 +00:00
|
|
|
common.Log.Debug("Parent obj is missing!! %T %p %v", v, v, v)
|
2016-12-06 01:18:44 +00:00
|
|
|
this.pendingObjects[v] = dict
|
2016-12-05 16:21:23 +00:00
|
|
|
// Although it is missing at this point, it could be added later...
|
2016-09-08 17:53:45 +00:00
|
|
|
}
|
2016-08-16 16:22:01 +00:00
|
|
|
// How to handle the parent? Make sure it is present?
|
2017-07-08 21:04:13 +00:00
|
|
|
if parentObj, parentIsRef := dict.Get("Parent").(*PdfObjectReference); parentIsRef {
|
2016-08-16 16:22:01 +00:00
|
|
|
// Parent is a reference. Means we can drop it?
|
|
|
|
// Could refer to somewhere outside of the scope of the output doc.
|
|
|
|
// Should be done by the reader already.
|
|
|
|
// -> ERROR.
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR: Parent is a reference object - Cannot be in writer (needs to be resolved)")
|
2016-08-16 16:22:01 +00:00
|
|
|
return fmt.Errorf("Parent is a reference object - Cannot be in writer (needs to be resolved) - %s", parentObj)
|
|
|
|
}
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if arr, isArray := obj.(*PdfObjectArray); isArray {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Array")
|
|
|
|
common.Log.Trace("- %s", obj)
|
2017-04-04 05:51:58 +00:00
|
|
|
if arr == nil {
|
|
|
|
return errors.New("Array is nil")
|
|
|
|
}
|
2018-07-15 17:52:53 +00:00
|
|
|
for _, v := range arr.Elements() {
|
2016-07-09 14:09:27 +00:00
|
|
|
err := this.addObjects(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, isReference := obj.(*PdfObjectReference); isReference {
|
|
|
|
// Should never be a reference, should already be resolved.
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR: Cannot be a reference!")
|
2016-07-09 14:09:27 +00:00
|
|
|
return errors.New("Reference not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a page to the PDF file. The new page should be an indirect
|
|
|
|
// object.
|
2016-12-13 19:40:33 +00:00
|
|
|
func (this *PdfWriter) AddPage(page *PdfPage) error {
|
|
|
|
obj := page.ToPdfObject()
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("==========")
|
|
|
|
common.Log.Trace("Appending to page list %T", obj)
|
2018-02-23 14:07:26 +00:00
|
|
|
procPage(page)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2016-12-13 19:40:33 +00:00
|
|
|
pageObj, ok := obj.(*PdfIndirectObject)
|
2016-07-09 14:09:27 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Page should be an indirect object")
|
|
|
|
}
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("%s", pageObj)
|
|
|
|
common.Log.Trace("%s", pageObj.PdfObject)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2016-12-13 19:40:33 +00:00
|
|
|
pDict, ok := pageObj.PdfObject.(*PdfObjectDictionary)
|
2016-07-17 21:42:09 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Page object should be a dictionary")
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
otype, ok := pDict.Get("Type").(*PdfObjectName)
|
2016-07-17 21:42:09 +00:00
|
|
|
if !ok {
|
2017-07-08 21:04:13 +00:00
|
|
|
return fmt.Errorf("Page should have a Type key with a value of type name (%T)", pDict.Get("Type"))
|
2016-09-12 17:59:45 +00:00
|
|
|
|
2016-07-17 21:42:09 +00:00
|
|
|
}
|
2016-07-09 14:09:27 +00:00
|
|
|
if *otype != "Page" {
|
|
|
|
return errors.New("Type != Page (Required).")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy inherited fields if missing.
|
|
|
|
inheritedFields := []PdfObjectName{"Resources", "MediaBox", "CropBox", "Rotate"}
|
2017-07-08 21:04:13 +00:00
|
|
|
parent, hasParent := pDict.Get("Parent").(*PdfIndirectObject)
|
|
|
|
common.Log.Trace("Page Parent: %T (%v)", pDict.Get("Parent"), hasParent)
|
2016-07-09 14:09:27 +00:00
|
|
|
for hasParent {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Page Parent: %T", parent)
|
2016-07-09 14:09:27 +00:00
|
|
|
parentDict, ok := parent.PdfObject.(*PdfObjectDictionary)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Invalid Parent object")
|
|
|
|
}
|
|
|
|
for _, field := range inheritedFields {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Field %s", field)
|
2017-07-08 21:04:13 +00:00
|
|
|
if pDict.Get(field) != nil {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("- page has already")
|
2016-07-09 14:09:27 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-08 21:04:13 +00:00
|
|
|
if obj := parentDict.Get(field); obj != nil {
|
2016-07-09 14:09:27 +00:00
|
|
|
// Parent has the field. Inherit, pass to the new page.
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Inheriting field %s", field)
|
2017-07-08 21:04:13 +00:00
|
|
|
pDict.Set(field, obj)
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
parent, hasParent = parentDict.Get("Parent").(*PdfIndirectObject)
|
|
|
|
common.Log.Trace("Next parent: %T", parentDict.Get("Parent"))
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Traversal done")
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Update the dictionary.
|
|
|
|
// Reuses the input object, updating the fields.
|
2017-07-08 21:04:13 +00:00
|
|
|
pDict.Set("Parent", this.pages)
|
2016-12-13 19:40:33 +00:00
|
|
|
pageObj.PdfObject = pDict
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Add to Pages.
|
2016-07-17 21:42:09 +00:00
|
|
|
pagesDict, ok := this.pages.PdfObject.(*PdfObjectDictionary)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Invalid Pages obj (not a dict)")
|
|
|
|
}
|
2017-07-08 21:04:13 +00:00
|
|
|
kids, ok := pagesDict.Get("Kids").(*PdfObjectArray)
|
2016-07-17 21:42:09 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Invalid Pages Kids obj (not an array)")
|
|
|
|
}
|
2018-07-15 17:52:53 +00:00
|
|
|
kids.Append(pageObj)
|
2017-07-08 21:04:13 +00:00
|
|
|
pageCount, ok := pagesDict.Get("Count").(*PdfObjectInteger)
|
2016-07-17 21:42:09 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("Invalid Pages Count object (not an integer)")
|
|
|
|
}
|
|
|
|
// Update the count.
|
2016-07-09 14:09:27 +00:00
|
|
|
*pageCount = *pageCount + 1
|
|
|
|
|
2016-12-13 19:40:33 +00:00
|
|
|
this.addObject(pageObj)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Traverse the page and record all object references.
|
|
|
|
err := this.addObjects(pDict)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-23 14:07:26 +00:00
|
|
|
func procPage(p *PdfPage) {
|
|
|
|
lk := license.GetLicenseKey()
|
|
|
|
if lk != nil && lk.IsLicensed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add font as needed.
|
|
|
|
f := fonts.NewFontHelvetica()
|
|
|
|
p.Resources.SetFontByName("UF1", f.ToPdfObject())
|
|
|
|
|
|
|
|
ops := []string{}
|
|
|
|
ops = append(ops, "q")
|
|
|
|
ops = append(ops, "BT")
|
|
|
|
ops = append(ops, "/UF1 14 Tf")
|
|
|
|
ops = append(ops, "1 0 0 rg")
|
|
|
|
ops = append(ops, "10 10 Td")
|
|
|
|
s := "Unlicensed UniDoc - Get a license on https://unidoc.io"
|
|
|
|
ops = append(ops, fmt.Sprintf("(%s) Tj", s))
|
|
|
|
ops = append(ops, "ET")
|
|
|
|
ops = append(ops, "Q")
|
|
|
|
contentstr := strings.Join(ops, "\n")
|
|
|
|
|
|
|
|
p.AddContentStreamByString(contentstr)
|
|
|
|
|
|
|
|
// Update page object.
|
|
|
|
p.ToPdfObject()
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:43:44 +00:00
|
|
|
// Add outlines to a PDF file.
|
|
|
|
func (this *PdfWriter) AddOutlineTree(outlineTree *PdfOutlineTreeNode) {
|
|
|
|
this.outlineTree = outlineTree
|
|
|
|
}
|
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
// Look for a specific key. Returns a list of entries.
|
|
|
|
// What if something appears on many pages?
|
|
|
|
func (this *PdfWriter) seekByName(obj PdfObject, followKeys []string, key string) ([]PdfObject, error) {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Seek by name.. %T", obj)
|
2016-07-09 14:09:27 +00:00
|
|
|
list := []PdfObject{}
|
|
|
|
if io, isIndirectObj := obj.(*PdfIndirectObject); isIndirectObj {
|
|
|
|
return this.seekByName(io.PdfObject, followKeys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if so, isStreamObj := obj.(*PdfObjectStream); isStreamObj {
|
|
|
|
return this.seekByName(so.PdfObjectDictionary, followKeys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dict, isDict := obj.(*PdfObjectDictionary); isDict {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Dict")
|
2017-07-08 21:04:13 +00:00
|
|
|
for _, k := range dict.Keys() {
|
|
|
|
v := dict.Get(k)
|
2016-07-09 14:09:27 +00:00
|
|
|
if string(k) == key {
|
|
|
|
list = append(list, v)
|
|
|
|
}
|
|
|
|
for _, followKey := range followKeys {
|
|
|
|
if string(k) == followKey {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Follow key %s", followKey)
|
2016-07-09 14:09:27 +00:00
|
|
|
items, err := this.seekByName(v, followKeys, key)
|
|
|
|
if err != nil {
|
|
|
|
return list, err
|
|
|
|
}
|
|
|
|
for _, item := range items {
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:05:20 +00:00
|
|
|
// Add Acroforms to a PDF file. Sets the specified form for writing.
|
|
|
|
func (this *PdfWriter) SetForms(form *PdfAcroForm) error {
|
2016-09-07 17:56:45 +00:00
|
|
|
this.acroForm = form
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
// Write out an indirect / stream object.
|
|
|
|
func (this *PdfWriter) writeObject(num int, obj PdfObject) {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Write obj #%d\n", num)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
if pobj, isIndirect := obj.(*PdfIndirectObject); isIndirect {
|
|
|
|
outStr := fmt.Sprintf("%d 0 obj\n", num)
|
|
|
|
outStr += pobj.PdfObject.DefaultWriteString()
|
|
|
|
outStr += "\nendobj\n"
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-22 21:10:57 +00:00
|
|
|
// XXX/TODO: Add a default encoder if Filter not specified?
|
|
|
|
// Still need to make sure is encrypted.
|
2016-07-09 14:09:27 +00:00
|
|
|
if pobj, isStream := obj.(*PdfObjectStream); isStream {
|
|
|
|
outStr := fmt.Sprintf("%d 0 obj\n", num)
|
|
|
|
outStr += pobj.PdfObjectDictionary.DefaultWriteString()
|
|
|
|
outStr += "\nstream\n"
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
this.writer.Write(pobj.Stream)
|
|
|
|
this.writer.WriteString("\nendstream\nendobj\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.writer.WriteString(obj.DefaultWriteString())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all the object numbers prior to writing.
|
|
|
|
func (this *PdfWriter) updateObjectNumbers() {
|
|
|
|
// Update numbers
|
|
|
|
for idx, obj := range this.objects {
|
|
|
|
if io, isIndirect := obj.(*PdfIndirectObject); isIndirect {
|
|
|
|
io.ObjectNumber = int64(idx + 1)
|
|
|
|
io.GenerationNumber = 0
|
|
|
|
}
|
|
|
|
if so, isStream := obj.(*PdfObjectStream); isStream {
|
|
|
|
so.ObjectNumber = int64(idx + 1)
|
|
|
|
so.GenerationNumber = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 16:40:39 +00:00
|
|
|
type EncryptOptions struct {
|
|
|
|
Permissions AccessPermissions
|
|
|
|
}
|
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
// Encrypt the output file with a specified user/owner password.
|
2016-07-09 16:40:39 +00:00
|
|
|
func (this *PdfWriter) Encrypt(userPass, ownerPass []byte, options *EncryptOptions) error {
|
2016-07-09 14:09:27 +00:00
|
|
|
crypter := PdfCrypt{}
|
|
|
|
this.crypter = &crypter
|
|
|
|
|
2016-09-09 15:02:52 +00:00
|
|
|
crypter.EncryptedObjects = map[PdfObject]bool{}
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2016-09-09 15:02:52 +00:00
|
|
|
crypter.CryptFilters = CryptFilters{}
|
|
|
|
crypter.CryptFilters["Default"] = CryptFilter{Cfm: "V2", Length: 128}
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Set
|
|
|
|
crypter.P = -1
|
|
|
|
crypter.V = 2
|
|
|
|
crypter.R = 3
|
2016-09-09 15:02:52 +00:00
|
|
|
crypter.Length = 128
|
|
|
|
crypter.EncryptMetadata = true
|
2016-07-09 16:40:39 +00:00
|
|
|
if options != nil {
|
|
|
|
crypter.P = int(options.Permissions.GetP())
|
|
|
|
}
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Prepare the ID object for the trailer.
|
|
|
|
hashcode := md5.Sum([]byte(time.Now().Format(time.RFC850)))
|
2018-07-14 02:25:29 +00:00
|
|
|
id0 := string(hashcode[:])
|
2016-07-09 14:09:27 +00:00
|
|
|
b := make([]byte, 100)
|
|
|
|
rand.Read(b)
|
|
|
|
hashcode = md5.Sum(b)
|
2018-07-14 02:25:29 +00:00
|
|
|
id1 := string(hashcode[:])
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Random b: % x", b)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2018-07-14 02:25:29 +00:00
|
|
|
this.ids = MakeArray(MakeHexString(id0), MakeHexString(id1))
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Gen Id 0: % x", id0)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2016-09-09 15:02:52 +00:00
|
|
|
crypter.Id0 = string(id0)
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Make the O and U objects.
|
2016-09-09 15:02:52 +00:00
|
|
|
O, err := crypter.Alg3(userPass, ownerPass)
|
2016-07-09 14:09:27 +00:00
|
|
|
if err != nil {
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR: Error generating O for encryption (%s)", err)
|
2016-07-09 14:09:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
crypter.O = []byte(O)
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("gen O: % x", O)
|
2016-09-09 15:02:52 +00:00
|
|
|
U, key, err := crypter.Alg5(userPass)
|
2016-07-09 14:09:27 +00:00
|
|
|
if err != nil {
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR: Error generating O for encryption (%s)", err)
|
2016-07-09 14:09:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("gen U: % x", U)
|
2016-07-09 14:09:27 +00:00
|
|
|
crypter.U = []byte(U)
|
2016-09-09 15:02:52 +00:00
|
|
|
crypter.EncryptionKey = key
|
2016-07-09 14:09:27 +00:00
|
|
|
|
|
|
|
// Generate the encryption dictionary.
|
2017-07-08 21:04:13 +00:00
|
|
|
encDict := MakeDict()
|
|
|
|
encDict.Set("Filter", MakeName("Standard"))
|
|
|
|
encDict.Set("P", MakeInteger(int64(crypter.P)))
|
|
|
|
encDict.Set("V", MakeInteger(int64(crypter.V)))
|
|
|
|
encDict.Set("R", MakeInteger(int64(crypter.R)))
|
|
|
|
encDict.Set("Length", MakeInteger(int64(crypter.Length)))
|
2018-07-14 02:25:29 +00:00
|
|
|
encDict.Set("O", MakeHexString(O))
|
|
|
|
encDict.Set("U", MakeHexString(U))
|
2016-07-09 14:09:27 +00:00
|
|
|
this.encryptDict = encDict
|
|
|
|
|
|
|
|
// Make an object to contain it.
|
2017-07-08 21:04:13 +00:00
|
|
|
io := MakeIndirectObject(encDict)
|
2016-07-09 14:09:27 +00:00
|
|
|
this.encryptObj = io
|
|
|
|
this.addObject(io)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the pdf out.
|
|
|
|
func (this *PdfWriter) Write(ws io.WriteSeeker) error {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Write()")
|
2018-02-23 14:07:26 +00:00
|
|
|
|
|
|
|
lk := license.GetLicenseKey()
|
|
|
|
if lk == nil || !lk.IsLicensed() {
|
|
|
|
fmt.Printf("Unlicensed copy of unidoc\n")
|
|
|
|
fmt.Printf("To get rid of the watermark - Please get a license on https://unidoc.io\n")
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:43:44 +00:00
|
|
|
// Outlines.
|
|
|
|
if this.outlineTree != nil {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("OutlineTree: %+v", this.outlineTree)
|
2016-09-11 23:17:38 +00:00
|
|
|
outlines := this.outlineTree.ToPdfObject()
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Outlines: %+v (%T, p:%p)", outlines, outlines, outlines)
|
2017-07-08 21:04:13 +00:00
|
|
|
this.catalog.Set("Outlines", outlines)
|
2016-08-18 09:43:44 +00:00
|
|
|
err := this.addObjects(outlines)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:05:20 +00:00
|
|
|
|
2016-08-18 15:55:20 +00:00
|
|
|
// Form fields.
|
2016-09-07 17:56:45 +00:00
|
|
|
if this.acroForm != nil {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Writing acro forms")
|
2016-09-11 23:17:38 +00:00
|
|
|
indObj := this.acroForm.ToPdfObject()
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("AcroForm: %+v", indObj)
|
2017-07-08 21:04:13 +00:00
|
|
|
this.catalog.Set("AcroForm", indObj)
|
2016-09-07 17:56:45 +00:00
|
|
|
err := this.addObjects(indObj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-07-09 14:09:27 +00:00
|
|
|
|
2016-12-05 16:21:23 +00:00
|
|
|
// Check pending objects prior to write.
|
2016-12-06 01:18:44 +00:00
|
|
|
for pendingObj, pendingObjDict := range this.pendingObjects {
|
2016-12-05 16:21:23 +00:00
|
|
|
if !this.hasObject(pendingObj) {
|
|
|
|
common.Log.Debug("ERROR Pending object %+v %T (%p) never added for writing", pendingObj, pendingObj, pendingObj)
|
2017-07-08 21:04:13 +00:00
|
|
|
for _, key := range pendingObjDict.Keys() {
|
|
|
|
val := pendingObjDict.Get(key)
|
2016-12-06 01:18:44 +00:00
|
|
|
if val == pendingObj {
|
|
|
|
common.Log.Debug("Pending object found! and replaced with null")
|
2017-07-08 21:04:13 +00:00
|
|
|
pendingObjDict.Set(key, MakeNull())
|
2016-12-06 01:18:44 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-12-05 16:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-28 08:58:50 +00:00
|
|
|
// Set version in the catalog.
|
2017-07-08 21:04:13 +00:00
|
|
|
this.catalog.Set("Version", MakeName(fmt.Sprintf("%d.%d", this.majorVersion, this.minorVersion)))
|
2016-12-05 16:21:23 +00:00
|
|
|
|
2016-07-09 14:09:27 +00:00
|
|
|
w := bufio.NewWriter(ws)
|
|
|
|
this.writer = w
|
|
|
|
|
2017-02-28 08:58:50 +00:00
|
|
|
w.WriteString(fmt.Sprintf("%%PDF-%d.%d\n", this.majorVersion, this.minorVersion))
|
2016-07-09 14:09:27 +00:00
|
|
|
w.WriteString("%âãÏÓ\n")
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
this.updateObjectNumbers()
|
|
|
|
|
|
|
|
offsets := []int64{}
|
|
|
|
|
|
|
|
// Write objects
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Writing %d obj", len(this.objects))
|
2016-07-09 14:09:27 +00:00
|
|
|
for idx, obj := range this.objects {
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Writing %d", idx)
|
2016-07-09 14:09:27 +00:00
|
|
|
this.writer.Flush()
|
|
|
|
offset, _ := ws.Seek(0, os.SEEK_CUR)
|
|
|
|
offsets = append(offsets, offset)
|
|
|
|
|
|
|
|
// Encrypt prior to writing.
|
|
|
|
// Encrypt dictionary should not be encrypted.
|
|
|
|
if this.crypter != nil && obj != this.encryptObj {
|
|
|
|
err := this.crypter.Encrypt(obj, int64(idx+1), 0)
|
|
|
|
if err != nil {
|
2016-10-31 21:48:25 +00:00
|
|
|
common.Log.Debug("ERROR: Failed encrypting (%s)", err)
|
2016-07-09 14:09:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
this.writeObject(idx+1, obj)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
xrefOffset, _ := ws.Seek(0, os.SEEK_CUR)
|
|
|
|
// Write xref table.
|
|
|
|
this.writer.WriteString("xref\r\n")
|
|
|
|
outStr := fmt.Sprintf("%d %d\r\n", 0, len(this.objects)+1)
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
outStr = fmt.Sprintf("%.10d %.5d f\r\n", 0, 65535)
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
for _, offset := range offsets {
|
|
|
|
outStr = fmt.Sprintf("%.10d %.5d n\r\n", offset, 0)
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate & write trailer
|
2017-07-08 21:04:13 +00:00
|
|
|
trailer := MakeDict()
|
|
|
|
trailer.Set("Info", this.infoObj)
|
|
|
|
trailer.Set("Root", this.root)
|
|
|
|
trailer.Set("Size", MakeInteger(int64(len(this.objects)+1)))
|
2016-07-09 14:09:27 +00:00
|
|
|
// If encrypted!
|
|
|
|
if this.crypter != nil {
|
2017-07-08 21:04:13 +00:00
|
|
|
trailer.Set("Encrypt", this.encryptObj)
|
|
|
|
trailer.Set("ID", this.ids)
|
2017-02-28 12:16:46 +00:00
|
|
|
common.Log.Trace("Ids: %s", this.ids)
|
2016-07-09 14:09:27 +00:00
|
|
|
}
|
|
|
|
this.writer.WriteString("trailer\n")
|
|
|
|
this.writer.WriteString(trailer.DefaultWriteString())
|
|
|
|
this.writer.WriteString("\n")
|
|
|
|
|
|
|
|
// Make offset reference.
|
|
|
|
outStr = fmt.Sprintf("startxref\n%d\n", xrefOffset)
|
|
|
|
this.writer.WriteString(outStr)
|
|
|
|
this.writer.WriteString("%%EOF\n")
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|