From 55370a856fb5610bafac951f72016f2c94f12800 Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Wed, 19 Aug 2015 19:13:07 +0100 Subject: Fhttp: add RegisterRemoteImage functionality. This is a wrapper around manually having to download the image and registering it. It registers an image based on it's URL, it will download that image and add it to the PDF. It will however not add it immediately to the page. This is done by calling `Image()` with the same URL. --- contrib/fhttp/fhttp.go | 26 ++++++++++++++++++++++++++ contrib/fhttp/fhttp_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ contrib/fhttp/pdf/.gitignore | 1 + contrib/fhttp/pdf/.gitkeep | 0 4 files changed, 70 insertions(+) create mode 100644 contrib/fhttp/fhttp.go create mode 100644 contrib/fhttp/fhttp_test.go create mode 100644 contrib/fhttp/pdf/.gitignore create mode 100644 contrib/fhttp/pdf/.gitkeep (limited to 'contrib') diff --git a/contrib/fhttp/fhttp.go b/contrib/fhttp/fhttp.go new file mode 100644 index 0000000..119e30e --- /dev/null +++ b/contrib/fhttp/fhttp.go @@ -0,0 +1,26 @@ +package fhttp + +import ( + "github.com/jung-kurt/gofpdf" + "net/http" +) + +// RegisterRemoteImage registers a remote image. Downloading the image from the +// provided URL and adding it to the PDF but not adding it to the page. Use +// Image() with the same URL to add the image to the page. +func RegisterRemoteImage(f *gofpdf.Fpdf, urlStr, tp string) (info *gofpdf.ImageInfoType) { + resp, err := http.Get(urlStr) + + if err != nil { + f.SetError(err) + return + } + + defer resp.Body.Close() + + if tp == "" { + tp = f.ImageTypeFromMime(resp.Header["Content-Type"][0]) + } + + return f.RegisterImageReader(urlStr, tp, resp.Body) +} diff --git a/contrib/fhttp/fhttp_test.go b/contrib/fhttp/fhttp_test.go new file mode 100644 index 0000000..57bf175 --- /dev/null +++ b/contrib/fhttp/fhttp_test.go @@ -0,0 +1,43 @@ +package fhttp_test + +import ( + "fmt" + "github.com/jung-kurt/gofpdf" + "github.com/jung-kurt/gofpdf/contrib/fhttp" + "path/filepath" +) + +const ( + cnGofpdfDir = "./" + cnExampleDir = cnGofpdfDir + "/pdf" +) + +func exampleFilename(baseStr string) string { + return filepath.Join(cnExampleDir, baseStr+".pdf") +} + +func summary(err error, fileStr string) { + if err == nil { + fileStr = filepath.ToSlash(fileStr) + fmt.Printf("Successfully generated %s\n", fileStr) + } else { + fmt.Println(err) + } +} + +func ExampleFpdf_AddRemoteImage() { + pdf := gofpdf.New("", "", "", "") + pdf.SetFont("Helvetica", "", 12) + pdf.SetFillColor(200, 200, 220) + pdf.AddPage() + + url := "https://github.com/jung-kurt/gofpdf/raw/master/image/logo_gofpdf.jpg?raw=true" + fhttp.RegisterRemoteImage(pdf, url, "") + pdf.Image(url, 100, 100, 20, 20, false, "", 0, "") + + fileStr := exampleFilename("Fpdf_AddRemoteImage") + err := pdf.OutputFileAndClose(fileStr) + summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_AddRemoteImage.pdf +} diff --git a/contrib/fhttp/pdf/.gitignore b/contrib/fhttp/pdf/.gitignore new file mode 100644 index 0000000..a136337 --- /dev/null +++ b/contrib/fhttp/pdf/.gitignore @@ -0,0 +1 @@ +*.pdf diff --git a/contrib/fhttp/pdf/.gitkeep b/contrib/fhttp/pdf/.gitkeep new file mode 100644 index 0000000..e69de29 -- cgit v1.2.1-24-ge1ad