From 8f46928acb094c8838f75d663b2f560dbf934773 Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Tue, 4 Aug 2015 08:20:25 +0100 Subject: Templating --- fpdf_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'fpdf_test.go') diff --git a/fpdf_test.go b/fpdf_test.go index 57a2dd4..c21b1ad 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -29,7 +29,7 @@ import ( "strconv" "strings" - "github.com/jung-kurt/gofpdf" + "github.com/marcusatbang/gofpdf" ) // Absolute path needed for gocov tool; relative OK for test @@ -1686,3 +1686,56 @@ func ExampleFpdf_DrawPath() { // Output: // Successfully generated pdf/Fpdf_DrawPath_fill.pdf } + +// This example demonstrates creating and using templates +func ExampleFpdf_CreateTemplate() { + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.SetCompression(false) + // pdf.SetFont("Times", "", 12) + template := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { + tpl.Image(imageFile("logo.png"), 6, 6, 30, 0, false, "", 0, "") + tpl.SetFont("Arial", "B", 16) + tpl.Text(40, 20, "Template says hello") + tpl.SetDrawColor(0, 100, 200) + tpl.SetLineWidth(2.5) + tpl.Line(95, 12, 105, 22) + }) + _, tplSize := template.Size() + // fmt.Println("Size:", tplSize) + // fmt.Println("Scaled:", tplSize.ScaleBy(1.5)) + + template2 := pdf.CreateTemplate(func(tpl *gofpdf.Tpl) { + tpl.UseTemplate(template) + subtemplate := tpl.CreateTemplate(func(tpl2 *gofpdf.Tpl) { + tpl2.Image(imageFile("logo.png"), 6, 86, 30, 0, false, "", 0, "") + tpl2.SetFont("Arial", "B", 16) + tpl2.Text(40, 100, "Subtemplate says hello") + tpl2.SetDrawColor(0, 200, 100) + tpl2.SetLineWidth(2.5) + tpl2.Line(102, 92, 112, 102) + }) + tpl.UseTemplate(subtemplate) + }) + + pdf.SetDrawColor(200, 100, 0) + pdf.SetLineWidth(2.5) + pdf.SetFont("Arial", "B", 16) + + pdf.AddPage() + pdf.UseTemplate(template) + pdf.UseTemplateScaled(template, gofpdf.PointType{0, 30}, tplSize) + pdf.UseTemplateScaled(template, gofpdf.PointType{0, 60}, tplSize.ScaleBy(1.4)) + pdf.Line(40, 210, 60, 210) + pdf.Text(40, 200, "Template example page 1") + + pdf.AddPage() + pdf.UseTemplate(template2) + pdf.Line(60, 210, 80, 210) + pdf.Text(40, 200, "Template example page 2") + + fileStr := exampleFilename("template") + err := pdf.OutputFileAndClose(fileStr) + summary(err, fileStr) + // Output: + // Successfully generated pdf/template.pdf +} -- cgit v1.2.1-24-ge1ad