From 6766a7e6d59e243ee86f02ff6646bdbc6464bbd8 Mon Sep 17 00:00:00 2001 From: Yash Kamothi Date: Wed, 27 Feb 2019 12:06:52 -0800 Subject: added functionality to change the current page in the pdf document --- .gitignore | 1 + def.go | 1 + fpdf.go | 10 ++++++++++ fpdf_test.go | 6 +++--- ttfparser_test.go | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 60e2227..b28eb22 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ private *.sublime* *.swp **/*.test +.idea/ diff --git a/def.go b/def.go index f19e86b..12b5edf 100644 --- a/def.go +++ b/def.go @@ -442,6 +442,7 @@ type Pdf interface { SetMargins(left, top, right float64) SetPageBoxRec(t string, pb PageBox) SetPageBox(t string, x, y, wd, ht float64) + SetPage(pageNum int) SetProtection(actionFlag byte, userPassStr, ownerPassStr string) SetRightMargin(margin float64) SetSubject(subjectStr string, isUTF8 bool) diff --git a/fpdf.go b/fpdf.go index d6fc123..f18f2ad 100644 --- a/fpdf.go +++ b/fpdf.go @@ -383,6 +383,12 @@ func (f *Fpdf) SetPageBoxRec(t string, pb PageBox) { func (f *Fpdf) SetPageBox(t string, x, y, wd, ht float64) { f.SetPageBoxRec(t, PageBox{SizeType{Wd: wd, Ht: ht}, PointType{X: x, Y: y}}) } +// SetPage sets the current page to that of a valid page in the PDF document. +func (f *Fpdf) SetPage(pageNum int) { + if (pageNum > 0) && (pageNum < len(f.pages)) { + f.page = pageNum + } +} // SetFontLocation sets the location in the file system of the font and font // definition files. @@ -670,6 +676,9 @@ func (f *Fpdf) AddPageFormat(orientationStr string, size SizeType) { if f.err != nil { return } + if f.page != len(f.pages)-1 { + f.page = len(f.pages)-1 + } if f.state == 0 { f.open() } @@ -684,6 +693,7 @@ func (f *Fpdf) AddPageFormat(orientationStr string, size SizeType) { fc := f.color.fill tc := f.color.text cf := f.colorFlag + if f.page > 0 { f.inFooter = true // Page footer avoid double call on footer. diff --git a/fpdf_test.go b/fpdf_test.go index d84de54..7d862fa 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -32,8 +32,8 @@ import ( "time" "github.com/jung-kurt/gofpdf" - "github.com/jung-kurt/gofpdf/internal/example" - "github.com/jung-kurt/gofpdf/internal/files" + "../gofpdf/internal/example" + "../gofpdf/internal/files" ) func init() { @@ -2381,4 +2381,4 @@ func ExampleFpdf_SubWrite() { example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SubWrite.pdf -} +} \ No newline at end of file diff --git a/ttfparser_test.go b/ttfparser_test.go index 3286db2..5aaf244 100644 --- a/ttfparser_test.go +++ b/ttfparser_test.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/jung-kurt/gofpdf" - "github.com/jung-kurt/gofpdf/internal/example" + "../gofpdf/internal/example" ) func ExampleTtfParse() { -- cgit v1.2.1-24-ge1ad From 316a528a8b2eb9016e36d3be2f6e04eb5eb673d5 Mon Sep 17 00:00:00 2001 From: Yash Kamothi Date: Wed, 27 Feb 2019 12:18:39 -0800 Subject: cleanup for pull request --- fpdf_test.go | 4 ++-- ttfparser_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fpdf_test.go b/fpdf_test.go index 7d862fa..09f4d39 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -32,8 +32,8 @@ import ( "time" "github.com/jung-kurt/gofpdf" - "../gofpdf/internal/example" - "../gofpdf/internal/files" + "github.com/jung-kurt/gofpdf/internal/example" + "github.com/jung-kurt/gofpdf/internal/files" ) func init() { diff --git a/ttfparser_test.go b/ttfparser_test.go index 5aaf244..3286db2 100644 --- a/ttfparser_test.go +++ b/ttfparser_test.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/jung-kurt/gofpdf" - "../gofpdf/internal/example" + "github.com/jung-kurt/gofpdf/internal/example" ) func ExampleTtfParse() { -- cgit v1.2.1-24-ge1ad From ac77cdcbc1c0005351dbea42cc07add42e1d3f7c Mon Sep 17 00:00:00 2001 From: ykamo001 Date: Wed, 27 Feb 2019 21:26:56 -0800 Subject: added example for SetPage method of fpdf --- fpdf_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/fpdf_test.go b/fpdf_test.go index 09f4d39..7969f94 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -23,6 +23,7 @@ import ( "io" "io/ioutil" "math" + "math/rand" "net/http" "os" "path/filepath" @@ -2381,4 +2382,57 @@ func ExampleFpdf_SubWrite() { example.Summary(err, fileStr) // Output: // Successfully generated pdf/Fpdf_SubWrite.pdf -} \ No newline at end of file +} + +func ExampleChangePage() { + pdf := gofpdf.New("P", "cm", "A4", "") + pdf.SetFont("Times", "", 12) + + var time []float64 + temperaturesFromSensors := make([][]float64, 5) + maxs := []float64{25, 41, 89, 62, 11} + for i := range temperaturesFromSensors { + temperaturesFromSensors[i] = make([]float64, 0) + } + + for i := 0.0; i < 100; i += 0.5 { + time = append(time, i) + for j, sensor := range temperaturesFromSensors { + dataValue := rand.Float64() * maxs[j] + sensor = append(sensor, dataValue) + temperaturesFromSensors[j] = sensor + } + } + var graphs []gofpdf.GridType + var pageNums []int + xMax := time[len(time)-1] + for i := range temperaturesFromSensors { + //Create a new page and graph for each sensor we want to graph. + pdf.AddPage() + pdf.Ln(1) + //Custom label per sensor + pdf.WriteAligned(0, 0, "Temperature Sensor "+strconv.Itoa(i+1)+" (C) vs Time (min)", "C") + pdf.Ln(0.5) + graph := gofpdf.NewGrid(pdf.GetX(), pdf.GetY(), 20, 10) + graph.TickmarksContainX(0, xMax) + //Custom Y axis + graph.TickmarksContainY(0, maxs[i]) + graph.Grid(pdf) + //Save references and locations. + graphs = append(graphs, graph) + pageNums = append(pageNums, pdf.PageNo()) + } + // For each X, graph the Y in each sensor. + for i, currTime := range time { + for j, sensor := range temperaturesFromSensors { + pdf.SetPage(pageNums[j]) + graph := graphs[j] + temperature := sensor[i] + pdf.Circle(graph.X(currTime), graph.Y(temperature), 0.04, "D") + } + } + + fileStr := example.Filename("Fpdf_SetPage") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) +} -- cgit v1.2.1-24-ge1ad