Skip to content

Commit

Permalink
Merge branch 'benoitkugler-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jung-kurt committed Nov 19, 2019
2 parents c86cd48 + e3aeeb1 commit 16955d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
3 changes: 2 additions & 1 deletion def.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 27 additions & 7 deletions fpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
Expand All @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions fpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 16955d8

Please sign in to comment.