From 950a9cdd97a64e35ef2b794a393fbab648f07f6f Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 19 Nov 2019 09:42:17 -0500 Subject: [PATCH] Merge benoitkugler's modification time pull request --- def.go | 3 ++- fpdf.go | 34 +++++++++++++++++++++++++++------- fpdf_test.go | 17 +++++++++++++++++ internal/example/example.go | 1 + 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/def.go b/def.go index 20c0ea30..6a7030f9 100644 --- a/def.go +++ b/def.go @@ -573,7 +573,8 @@ type Fpdf struct { author string // author keywords string // keywords creator string // creator - creationDate time.Time // override for dcoument CreationDate value + creationDate time.Time // override for document CreationDate value + modDate time.Time // override for document ModDate value aliasNbPagesStr string // alias for total number of pages pdfVersion string // PDF version number fontDirStr string // location of font definition files diff --git a/fpdf.go b/fpdf.go index 2d910583..46651cbb 100644 --- a/fpdf.go +++ b/fpdf.go @@ -46,6 +46,7 @@ var gl struct { catalogSort bool noCompress bool // Initial zero value indicates compression creationDate time.Time + modDate time.Time } type fmtBuffer struct { @@ -205,6 +206,7 @@ func fpdfNew(orientationStr, unitStr, sizeStr, fontDirStr string, size SizeType) f.layerInit() f.catalogSort = gl.catalogSort f.creationDate = gl.creationDate + f.modDate = gl.modDate f.userUnderlineThickness = 1 return } @@ -3810,6 +3812,13 @@ func SetDefaultCreationDate(tm time.Time) { gl.creationDate = tm } +// SetDefaultModificationDate sets the default value of the document modification date +// that will be used when initializing a new Fpdf instance. See +// SetCreationDate() for more details. +func SetDefaultModificationDate(tm time.Time) { + gl.modDate = tm +} + // SetCreationDate fixes the document's internal CreationDate value. By // default, the time when the document is generated is used for this value. // This method is typically only used for testing purposes to facilitate PDF @@ -3818,6 +3827,12 @@ func (f *Fpdf) SetCreationDate(tm time.Time) { f.creationDate = tm } +// SetModificationDate fixes the document's internal ModDate value. +// See `SetCreationDate` for more details. +func (f *Fpdf) SetModificationDate(tm time.Time) { + f.modDate = tm +} + // SetJavascript adds Adobe JavaScript to the document. func (f *Fpdf) SetJavascript(script string) { f.javascript = &script @@ -4595,8 +4610,15 @@ func (f *Fpdf) putresources() { return } +// returns Now() if tm is zero +func timeOrNow(tm time.Time) time.Time { + if tm.IsZero() { + return time.Now() + } + return tm +} + func (f *Fpdf) putinfo() { - var tm time.Time if len(f.producer) > 0 { f.outf("/Producer %s", f.textstring(f.producer)) } @@ -4615,12 +4637,10 @@ func (f *Fpdf) putinfo() { if len(f.creator) > 0 { f.outf("/Creator %s", f.textstring(f.creator)) } - if f.creationDate.IsZero() { - tm = time.Now() - } else { - tm = f.creationDate - } - f.outf("/CreationDate %s", f.textstring("D:"+tm.Format("20060102150405"))) + creation := timeOrNow(f.creationDate) + f.outf("/CreationDate %s", f.textstring("D:"+creation.Format("20060102150405"))) + mod := timeOrNow(f.modDate) + f.outf("/ModDate %s", f.textstring("D:"+mod.Format("20060102150405"))) } func (f *Fpdf) putcatalog() { diff --git a/fpdf_test.go b/fpdf_test.go index bd0ef1d5..2cf1d1fc 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2377,6 +2377,7 @@ func ExampleFpdf_SetPageBox() { // ~ pdfinfo -box pdf/Fpdf_PageBox.pdf // Producer: FPDF 1.7 // CreationDate: Sat Jan 1 00:00:00 2000 + // ModDate: Sat Jan 1 00:00:00 2000 // Tagged: no // Form: none // Pages: 1 @@ -2886,3 +2887,19 @@ func ExampleFpdf_AddAttachmentAnnotation() { // Output: // Successfully generated pdf/Fpdf_FileAnnotations.pdf } + +func ExampleFpdf_SetModificationDate() { + // pdfinfo (from http://www.xpdfreader.com) reports the following for this example : + // ~ pdfinfo -box pdf/Fpdf_PageBox.pdf + // Producer: FPDF 1.7 + // CreationDate: Sat Jan 1 00:00:00 2000 + // ModDate: Sun Jan 2 10:22:30 2000 + pdf := gofpdf.New("", "", "", "") + pdf.AddPage() + pdf.SetModificationDate(time.Date(2000, 1, 2, 10, 22, 30, 0, time.UTC)) + fileStr := example.Filename("Fpdf_SetModificationDate") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_SetModificationDate.pdf +} diff --git a/internal/example/example.go b/internal/example/example.go index e9eb2a35..2667c4d1 100644 --- a/internal/example/example.go +++ b/internal/example/example.go @@ -34,6 +34,7 @@ func init() { gofpdf.SetDefaultCompression(false) gofpdf.SetDefaultCatalogSort(true) gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) + gofpdf.SetDefaultModificationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)) } // setRoot assigns the relative path to the gofpdfDir directory based on current working