From 892fe1b63b9a9f0b7bcdcdcdeb96cb6fe0d0a2bb Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 15 Apr 2018 14:15:23 -0400 Subject: Add SetHeaderFuncMode() method to automatically set position to left and top margin after header function is called. Backward compatibility is preserved. This addresses issue #174. --- def.go | 1 + fpdf.go | 19 +++++++++++++++++++ fpdf_test.go | 5 +++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/def.go b/def.go index 53bd90a..d53e627 100644 --- a/def.go +++ b/def.go @@ -321,6 +321,7 @@ type Fpdf struct { pageBreakTrigger float64 // threshold used to trigger page breaks inHeader bool // flag set when processing header headerFnc func() // function provided by app and called to write header + headerHomeMode bool // set position to home after headerFnc is called inFooter bool // flag set when processing footer footerFnc func() // function provided by app and called to write footer footerFncLpi func(bool) // function provided by app and called to write footer with last page flag diff --git a/fpdf.go b/fpdf.go index 4afdb8c..0e7071f 100644 --- a/fpdf.go +++ b/fpdf.go @@ -350,6 +350,15 @@ func (f *Fpdf) SetFontLoader(loader FontLoader) { f.fontLoader = loader } +// SetHeaderFuncMode sets the function that lets the application render the +// page header. See SetHeaderFunc() for more details. The value for homeMode +// should be set to true to have the current position set to the left and top +// margin after the header function is called. +func (f *Fpdf) SetHeaderFuncMode(fnc func(), homeMode bool) { + f.headerFnc = fnc + f.headerHomeMode = homeMode +} + // SetHeaderFunc sets the function that lets the application render the page // header. The specified function is automatically called by AddPage() and // should not be called directly by the application. The implementation in Fpdf @@ -676,6 +685,9 @@ func (f *Fpdf) AddPageFormat(orientationStr string, size SizeType) { f.inHeader = true f.headerFnc() f.inHeader = false + if f.headerHomeMode { + f.SetHomeXY() + } } // Restore line width if f.lineWidth != lw { @@ -2702,6 +2714,13 @@ func (f *Fpdf) SetY(y float64) { } } +// SetHomeXY is a convenience method that sets the current position to the left +// and top margins. +func (f *Fpdf) SetHomeXY() { + f.SetY(f.tMargin) + f.SetX(f.lMargin) +} + // SetXY defines the abscissa and ordinate of the current position. If the // passed values are negative, they are relative respectively to the right and // bottom of the page. diff --git a/fpdf_test.go b/fpdf_test.go index bbeb5b0..9f0ee96 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -206,14 +206,15 @@ func Example() { // This example demonsrates the generation of headers, footers and page breaks. func ExampleFpdf_AddPage() { pdf := gofpdf.New("P", "mm", "A4", "") - pdf.SetHeaderFunc(func() { + pdf.SetTopMargin(30) + pdf.SetHeaderFuncMode(func() { pdf.Image(example.ImageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "") pdf.SetY(5) pdf.SetFont("Arial", "B", 15) pdf.Cell(80, 0, "") pdf.CellFormat(30, 10, "Title", "1", 0, "C", false, 0, "") pdf.Ln(20) - }) + }, true) pdf.SetFooterFunc(func() { pdf.SetY(-15) pdf.SetFont("Arial", "I", 8) -- cgit v1.2.1-24-ge1ad