Support parsing and getting the actual version number and changing the output PDF version

This commit is contained in:
Gunnsteinn Hall 2017-02-28 08:58:50 +00:00
parent 3a6a4bea52
commit f1ada05914
2 changed files with 41 additions and 12 deletions

View File

@ -21,7 +21,7 @@ import (
)
// Regular Expressions for parsing and identifying object signatures.
var rePdfVersion = regexp.MustCompile(`%PDF-(\d\.\d)`)
var rePdfVersion = regexp.MustCompile(`%PDF-(\d)\.(\d)`)
var reEOF = regexp.MustCompile("%%EOF")
var reXrefTable = regexp.MustCompile(`\s*xref\s*`)
var reStartXref = regexp.MustCompile(`startx?ref\s*(\d+)`)
@ -33,6 +33,9 @@ var reXrefSubsection = regexp.MustCompile(`(\d+)\s+(\d+)\s*$`)
var reXrefEntry = regexp.MustCompile(`(\d+)\s+(\d+)\s+([nf])\s*$`)
type PdfParser struct {
majorVersion int
minorVersion int
rs io.ReadSeeker
reader *bufio.Reader
xrefs XrefTable
@ -631,27 +634,34 @@ func (this *PdfParser) ParseDict() (*PdfObjectDictionary, error) {
}
// Parse the pdf version from the beginning of the file.
func (this *PdfParser) parsePdfVersion() (float64, error) {
// Returns the major and minor parts of the version.
// E.g. for "PDF-1.7" would return 1 and 7.
func (this *PdfParser) parsePdfVersion() (int, int, error) {
this.rs.Seek(0, os.SEEK_SET)
var offset int64 = 20
b := make([]byte, offset)
this.rs.Read(b)
result1 := rePdfVersion.FindStringSubmatch(string(b))
if len(result1) < 2 {
if len(result1) < 3 {
common.Log.Debug("Error: PDF Version not found!")
return -1, errors.New("PDF version not found")
return 0, 0, errors.New("PDF version not found")
}
version, err := strconv.ParseFloat(result1[1], 64)
majorVersion, err := strconv.ParseInt(result1[1], 10, 64)
if err != nil {
return 0, err
return 0, 0, err
}
minorVersion, err := strconv.ParseInt(result1[2], 10, 64)
if err != nil {
return 0, 0, err
}
//version, _ := strconv.Atoi(result1[1])
common.Log.Debug("Pdf version %f", version)
common.Log.Debug("Pdf version %d.%d", majorVersion, minorVersion)
return version, nil
return int(majorVersion), int(minorVersion), nil
}
// Conventional xref table starting with 'xref'.
@ -1321,10 +1331,13 @@ func NewParser(rs io.ReadSeeker) (*PdfParser, error) {
// printXrefTable(parser.xrefs)
_, err = parser.parsePdfVersion()
majorVersion, minorVersion, err := parser.parsePdfVersion()
if err != nil {
return nil, fmt.Errorf("Unable to parse version (%s)", err)
common.Log.Error("Unable to parse version: %v", err)
return nil, err
}
parser.majorVersion = majorVersion
parser.minorVersion = minorVersion
parser.trailer = trailer

View File

@ -70,6 +70,10 @@ type PdfWriter struct {
encryptObj *PdfIndirectObject
ids *PdfObjectArray
// PDF version
majorVersion int
minorVersion int
// Objects to be followed up on prior to writing.
// These are objects that are added and reference objects that are not included
// for writing.
@ -88,6 +92,11 @@ func NewPdfWriter() PdfWriter {
w.objects = []PdfObject{}
w.pendingObjects = map[PdfObject]*PdfObjectDictionary{}
// 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
// Creation info.
infoDict := PdfObjectDictionary{}
infoDict[PdfObjectName("Producer")] = MakeString(getPdfProducer())
@ -101,7 +110,6 @@ func NewPdfWriter() PdfWriter {
catalog := PdfIndirectObject{}
catalogDict := PdfObjectDictionary{}
catalogDict[PdfObjectName("Type")] = MakeName("Catalog")
catalogDict[PdfObjectName("Version")] = MakeName("1.3")
catalog.PdfObject = &catalogDict
w.root = &catalog
@ -127,6 +135,12 @@ func NewPdfWriter() PdfWriter {
return w
}
// Set the PDF version of the output file.
func (this *PdfWriter) SetVersion(majorVersion, minorVersion int) {
this.majorVersion = majorVersion
this.minorVersion = minorVersion
}
// Set the optional content properties.
func (this *PdfWriter) SetOCProperties(ocProperties PdfObject) error {
dict := this.catalog
@ -638,11 +652,13 @@ func (this *PdfWriter) Write(ws io.WriteSeeker) error {
}
}
}
// Set version in the catalog.
(*this.catalog)["Version"] = MakeName(fmt.Sprintf("%d.%d", this.majorVersion, this.minorVersion))
w := bufio.NewWriter(ws)
this.writer = w
w.WriteString("%PDF-1.3\n")
w.WriteString(fmt.Sprintf("%%PDF-%d.%d\n", this.majorVersion, this.minorVersion))
w.WriteString("%âãÏÓ\n")
w.Flush()