summaryrefslogtreecommitdiff
path: root/fpdf_test.go
diff options
context:
space:
mode:
authorKurt Jung <kurt.w.jung@code.google.com>2014-10-12 14:56:43 -0400
committerKurt Jung <kurt.w.jung@code.google.com>2014-10-12 14:56:43 -0400
commit052c886766c341b3c4222b514c9b7bb496fe465e (patch)
tree11a8acc27e0d3529c472a309e5b37e5de3267dc4 /fpdf_test.go
parent03177bf963088c765c949168f91e6878d1a7bbb5 (diff)
Add example that demonstrates how to embed an image that is retrieved from a web server. Resolves issue 29.
Diffstat (limited to 'fpdf_test.go')
-rw-r--r--fpdf_test.go44
1 files changed, 43 insertions, 1 deletions
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
+
}