diff options
-rw-r--r-- | def.go | 1 | ||||
-rw-r--r-- | fpdf.go | 19 | ||||
-rw-r--r-- | fpdf_test.go | 5 |
3 files changed, 23 insertions, 2 deletions
@@ -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 @@ -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) |