diff options
author | Kurt Jung <kurt.w.jung@code.google.com> | 2014-02-17 15:55:44 -0500 |
---|---|---|
committer | Kurt Jung <kurt.w.jung@code.google.com> | 2014-02-17 15:55:44 -0500 |
commit | 79de4196673a7d880e08c1ba31fa8243004f9dbf (patch) | |
tree | 1a2f334ef9d4125502487a46243ea62a7c85aede | |
parent | 54926dff7aced06e420e30acacf942650e7a6e59 (diff) |
Added convenience function to write PDF to a file (rather than a Writer interface) and then close the PDF instance.
-rw-r--r-- | doc.go | 4 | ||||
-rw-r--r-- | fpdf.go | 28 | ||||
-rw-r--r-- | fpdf_test.go | 8 |
3 files changed, 31 insertions, 9 deletions
@@ -26,7 +26,7 @@ Features • Automatic page breaks, line breaks, and text justification -• Inclusion of JPEG, PNG and GIF images +• Inclusion of JPEG, PNG, GIF and basic path-only SVG images • Colors, gradients and alpha channel transparency @@ -44,8 +44,6 @@ Features • Clipping -• Basic path-only SVG images - gofpdf has no dependencies other than the Go standard library. All tests pass on Linux, Mac and Windows platforms. Like FPDF version 1.7, from which gofpdf is derived, this package does not yet support UTF-8 source text. @@ -34,6 +34,7 @@ import ( "io" "io/ioutil" "math" + "os" "path" "strings" "time" @@ -439,9 +440,9 @@ func (f *Fpdf) open() { } // Close terminates the PDF document. It is not necessary to call this method -// explicitly because Output() and OutputAndClose() do it automatically. If the -// document contains no page, AddPage() is called to prevent the generation of -// an invalid document. +// explicitly because Output(), OutputAndClose() and OutputFileAndClose() do it +// automatically. If the document contains no page, AddPage() is called to +// prevent the generation of an invalid document. func (f *Fpdf) Close() { if f.err == nil { if f.clipNest > 0 { @@ -2109,14 +2110,31 @@ func (f *Fpdf) SetXY(x, y float64) { f.SetX(x) } -// OutputAndClose send the PDF document to the writer specified by w. This method will close -// both f and w, even if an error is detected and no document is produced. +// OutputAndClose sends the PDF document to the writer specified by w. This +// method will close both f and w, even if an error is detected and no document +// is produced. func (f *Fpdf) OutputAndClose(w io.WriteCloser) error { f.Output(w) w.Close() return f.err } +// OutputFileAndClose creates or truncates the file specified by fileStr and +// writes the PDF document to it. This method will close f and the newly +// written file, even if an error is detected and no document is produced. +func (f *Fpdf) OutputFileAndClose(fileStr string) error { + if f.err == nil { + pdfFile, err := os.Create(fileStr) + if err == nil { + f.Output(pdfFile) + pdfFile.Close() + } else { + f.err = err + } + } + return f.err +} + // Output sends the PDF document to the writer specified by w. No output will // take place if an error has occured in the document generation process. w // remains open after this function returns. After returning, f is in a closed diff --git a/fpdf_test.go b/fpdf_test.go index aaf5807..be55031 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -124,7 +124,13 @@ func ExampleFpdf_tutorial01() { pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello World!") - pdf.OutputAndClose(docWriter(pdf, 1)) + fileStr := filepath.Join(cnGofpdfDir, "pdf/tutorial01.pdf") + err := pdf.OutputFileAndClose(fileStr) + if err == nil { + fmt.Println("Successfully generated pdf/tutorial01.pdf") + } else { + fmt.Println(err) + } // Output: // Successfully generated pdf/tutorial01.pdf } |