From 052c886766c341b3c4222b514c9b7bb496fe465e Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Sun, 12 Oct 2014 14:56:43 -0400 Subject: Add example that demonstrates how to embed an image that is retrieved from a web server. Resolves issue 29. --- fpdf_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'fpdf_test.go') diff --git a/fpdf_test.go b/fpdf_test.go index 41f388e..11b96bb 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io/ioutil" "math" + "net/http" "os" "path/filepath" "strings" @@ -1281,7 +1282,48 @@ func ExampleFpdf_tutorial26() { pdf.EndLayer() pdf.OutputAndClose(docWriter(pdf, 26)) - // Output: // Successfully generated pdf/tutorial26.pdf + +} + +// This example demonstrates the use of an image that is retrieved from a web +// server. +func ExampleFpdf_tutorial27() { + + const ( + margin = 10 + wd = 210 + ht = 297 + fontSize = 15 + urlStr = "https://code.google.com/p/gofpdf/logo?cct=1402750750" + msgStr = `Images from the web can be easily embedded when a PDF document is generated.` + ) + + var ( + rsp *http.Response + err error + tp string + ) + + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.SetFont("Helvetica", "", fontSize) + ln := pdf.PointConvert(fontSize) + pdf.MultiCell(wd-margin-margin, ln, msgStr, "", "L", false) + rsp, err = http.Get(urlStr) + if err == nil { + tp = pdf.ImageTypeFromMime(rsp.Header["Content-Type"][0]) + infoPtr := pdf.RegisterImageReader(urlStr, tp, rsp.Body) + if pdf.Ok() { + imgWd, imgHt := infoPtr.Extent() + pdf.Image(urlStr, (wd-imgWd)/2.0, pdf.GetY()+ln, imgWd, imgHt, false, tp, 0, "") + } + } else { + pdf.SetError(err) + } + pdf.OutputAndClose(docWriter(pdf, 27)) + // Output: + // Successfully generated pdf/tutorial27.pdf + } -- cgit v1.2.1-24-ge1ad