summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fpdf.go14
-rw-r--r--fpdf_test.go77
2 files changed, 62 insertions, 29 deletions
diff --git a/fpdf.go b/fpdf.go
index 55bb848..ea7c373 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -330,6 +330,8 @@ func (f *Fpdf) SetFontLoader(loader FontLoader) {
// is empty, so you have to provide an appropriate function if you want page
// headers. fnc will typically be a closure that has access to the Fpdf
// instance and other document generation variables.
+//
+// This method is demonstrated in the example for AddPage().
func (f *Fpdf) SetHeaderFunc(fnc func()) {
f.headerFnc = fnc
}
@@ -340,6 +342,8 @@ func (f *Fpdf) SetHeaderFunc(fnc func()) {
// implementation in Fpdf is empty, so you have to provide an appropriate
// function if you want page footers. fnc will typically be a closure that has
// access to the Fpdf instance and other document generation variables.
+//
+// This method is demonstrated in the example for AddPage().
func (f *Fpdf) SetFooterFunc(fnc func()) {
f.footerFnc = fnc
}
@@ -469,6 +473,8 @@ func (f *Fpdf) SetCreator(creatorStr string, isUTF8 bool) {
// AliasNbPages defines an alias for the total number of pages. It will be
// substituted as the document is closed. An empty string is replaced with the
// string "{nb}". This method is demonstrated in tutorial 2.
+//
+// See the example for AddPage() for a demonstration of this method.
func (f *Fpdf) AliasNbPages(aliasStr string) {
if aliasStr == "" {
aliasStr = "{nb}"
@@ -657,6 +663,8 @@ func (f *Fpdf) AddPage() {
}
// PageNo returns the current page number.
+//
+// See the example for AddPage() for a demonstration of this method.
func (f *Fpdf) PageNo() int {
return f.page
}
@@ -1640,8 +1648,8 @@ func (f *Fpdf) Text(x, y float64, txtStr string) {
// mode selected by SetAutoPageBreak. The function provided should not be
// called by the application.
//
-// See tutorial 4 for an example of how this function can be used to manage
-// multiple columns.
+// See the example for SetLeftMargin() to see how this function can be used to
+// manage multiple columns.
func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
f.acceptPageBreak = fnc
}
@@ -2115,6 +2123,8 @@ func (f *Fpdf) WriteLinkID(h float64, displayStr string, linkID int) {
// Ln performs a line break. The current abscissa goes back to the left margin
// and the ordinate increases by the amount passed in parameter. A negative
// value of h indicates the height of the last printed cell.
+//
+// This method is demonstrated in the example for MultiCell.
func (f *Fpdf) Ln(h float64) {
f.x = f.lMargin
if h < 0 {
diff --git a/fpdf_test.go b/fpdf_test.go
index 3872331..eaed7d8 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -34,10 +34,11 @@ import (
// Absolute path needed for gocov tool; relative OK for test
const (
- cnGofpdfDir = "."
- cnFontDir = cnGofpdfDir + "/font"
- cnImgDir = cnGofpdfDir + "/image"
- cnTextDir = cnGofpdfDir + "/text"
+ cnGofpdfDir = "."
+ cnFontDir = cnGofpdfDir + "/font"
+ cnImgDir = cnGofpdfDir + "/image"
+ cnTextDir = cnGofpdfDir + "/text"
+ cnExampleDir = cnGofpdfDir + "/pdf"
)
type nullWriter struct {
@@ -105,6 +106,10 @@ func textFile(fileStr string) string {
return filepath.Join(cnTextDir, fileStr)
}
+func exampleFile(fileStr string) string {
+ return filepath.Join(cnExampleDir, fileStr)
+}
+
type fontResourceType struct {
}
@@ -137,27 +142,37 @@ func lorem() string {
"officia deserunt mollit anim id est laborum."
}
-// Hello, world. Note that since only core fonts are used (in this case Arial,
-// a synonym for Helvetica), an empty string can be specified for the font
-// directory in the call to New().
-func ExampleFpdf_tutorial01() {
+func exampleFilename(baseStr string) string {
+ return filepath.Join(cnExampleDir, baseStr+".pdf")
+}
+
+func summary(err error, fileStr string) {
+ if err == nil {
+ fmt.Printf("Successfully generated %s\n", fileStr)
+ } else {
+ fmt.Println(err)
+ }
+}
+
+// This example demonstrates the generation of a simple PDF document. Note that
+// since only core fonts are used (in this case Arial, a synonym for
+// Helvetica), an empty string can be specified for the font directory in the
+// call to New(). Note also that the exampleFilename and summary functions are
+// local to the test file and are not part of the gofpdf library.
+func Example() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, "Hello World!")
- fileStr := filepath.Join(cnGofpdfDir, "pdf/tutorial01.pdf")
+ fileStr := exampleFilename("basic")
err := pdf.OutputFileAndClose(fileStr)
- if err == nil {
- fmt.Println("Successfully generated pdf/tutorial01.pdf")
- } else {
- fmt.Println(err)
- }
+ summary(err, fileStr)
// Output:
- // Successfully generated pdf/tutorial01.pdf
+ // Successfully generated pdf/basic.pdf
}
-// Header, footer and page-breaking
-func ExampleFpdf_tutorial02() {
+// This example demonsrates the generation of headers, footers and page breaks.
+func ExampleFpdf_AddPage() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetHeaderFunc(func() {
pdf.Image(imageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "")
@@ -178,13 +193,16 @@ func ExampleFpdf_tutorial02() {
for j := 1; j <= 40; j++ {
pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j), "", 1, "", false, 0, "")
}
- pdf.OutputAndClose(docWriter(pdf, 2))
+ fileStr := exampleFilename("addpage")
+ err := pdf.OutputFileAndClose(fileStr)
+ summary(err, fileStr)
// Output:
- // Successfully generated pdf/tutorial02.pdf
+ // Successfully generated pdf/addpage.pdf
}
-// Word-wrapping, line justification and page-breaking
-func ExampleFpdf_tutorial03() {
+// This example demonstrates word-wrapping, line justification and
+// page-breaking.
+func ExampleFpdf_MultiCell() {
pdf := gofpdf.New("P", "mm", "A4", "")
titleStr := "20000 Leagues Under the Seas"
pdf.SetTitle(titleStr, false)
@@ -249,13 +267,16 @@ func ExampleFpdf_tutorial03() {
}
printChapter(1, "A RUNAWAY REEF", textFile("20k_c1.txt"))
printChapter(2, "THE PROS AND CONS", textFile("20k_c2.txt"))
- pdf.OutputAndClose(docWriter(pdf, 3))
+ fileStr := exampleFilename("multicell")
+ err := pdf.OutputFileAndClose(fileStr)
+ summary(err, fileStr)
// Output:
- // Successfully generated pdf/tutorial03.pdf
+ // Successfully generated pdf/multicell.pdf
}
-// Multiple column layout
-func ExampleFpdf_tutorial04() {
+// This example demonstrates the generation of a PDF document that has multiple
+// columns. This is accomplished with the SetLeftMargin() and Cell() methods.
+func ExampleFpdf_SetLeftMargin() {
var y0 float64
var crrntCol int
pdf := gofpdf.New("P", "mm", "A4", "")
@@ -350,9 +371,11 @@ func ExampleFpdf_tutorial04() {
})
printChapter(1, "A RUNAWAY REEF", textFile("20k_c1.txt"))
printChapter(2, "THE PROS AND CONS", textFile("20k_c2.txt"))
- pdf.OutputAndClose(docWriter(pdf, 4))
+ fileStr := exampleFilename("multicolumn")
+ err := pdf.OutputFileAndClose(fileStr)
+ summary(err, fileStr)
// Output:
- // Successfully generated pdf/tutorial04.pdf
+ // Successfully generated pdf/multicolumn.pdf
}
// Various table styles