From a1dcf58b5248a95377100a535eb7d20f708cf6a0 Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Wed, 26 Aug 2015 19:43:02 +0100 Subject: Httpimg: rename package. --- contrib/httpimg/httpimg.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 contrib/httpimg/httpimg.go (limited to 'contrib/httpimg/httpimg.go') diff --git a/contrib/httpimg/httpimg.go b/contrib/httpimg/httpimg.go new file mode 100644 index 0000000..6025469 --- /dev/null +++ b/contrib/httpimg/httpimg.go @@ -0,0 +1,32 @@ +package httpimg + +import ( + "github.com/jung-kurt/gofpdf" + "net/http" +) + +// RegisterHttpImage registers a HTTP 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 RegisterHttpImage(f *gofpdf.Fpdf, urlStr, tp string) (info *gofpdf.ImageInfoType) { + info = f.GetImageInfo(urlStr) + + if info != nil { + return + } + + 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) +} -- cgit v1.2.1-24-ge1ad