From a8d8f9e6a80f014f8f5a2388fbf7f409fe9cf263 Mon Sep 17 00:00:00 2001 From: d1ngd0 Date: Wed, 12 Dec 2018 14:04:24 -0600 Subject: Added the ability to have multiple pages in a template (#216) * Added the ability to have multiple pages in a template Templates are now aware of the number of pages they have. The main additions are the FromPage and FromPages methods which allow you to extract a Template from a template if you pass in the page number. It follows the same page mechanism as FPDF where pages start in the index 1. * Update fpdf_test.go --- fpdf_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'fpdf_test.go') diff --git a/fpdf_test.go b/fpdf_test.go index 3e731fd..2fbca9c 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -64,6 +64,42 @@ func TestFpdfImplementPdf(t *testing.T) { var _ gofpdf.Pdf = (*gofpdf.Tpl)(nil) } +// TestPagedTemplate ensures new paged templates work +func TestPagedTemplate(t *testing.T) { + pdf := gofpdf.New("P", "mm", "A4", "") + tpl := pdf.CreateTemplate(func(t *gofpdf.Tpl) { + // this will be the second page, as a page is already + // created by default + t.AddPage() + t.AddPage() + t.AddPage() + }) + + if tpl.NumPages() != 4 { + t.Fatalf("The template does not have the correct number of pages %d", tpl.NumPages()) + } + + tplPages := tpl.FromPages() + for x := 0; x < len(tplPages); x++ { + pdf.AddPage() + pdf.UseTemplate(tplPages[x]) + } + + // get the last template + tpl2, err := tpl.FromPage(tpl.NumPages()) + if err != nil { + t.Fatal(err) + } + + // the objects should be the exact same, as the + // template will represent the last page by default + // therefore no new id should be set, and the object + // should be the same object + if fmt.Sprintf("%p", tpl2) != fmt.Sprintf("%p", tpl) { + t.Fatal("Template no longer respecting initial template object") + } +} + // TestIssue0116 addresses issue 116 in which library silently fails after // calling CellFormat when no font has been set. func TestIssue0116(t *testing.T) { -- cgit v1.2.1-24-ge1ad