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 From 74253e5c9ca7edeee98bcf4b38d9ef1d00fe7a0e Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Wed, 26 Aug 2015 19:44:21 +0100 Subject: HttpImg: use `Register` method. By changing the name, we can use a uniform name across several packages. --- contrib/httpimg/httpimg.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'contrib/httpimg/httpimg.go') diff --git a/contrib/httpimg/httpimg.go b/contrib/httpimg/httpimg.go index 6025469..10afcc8 100644 --- a/contrib/httpimg/httpimg.go +++ b/contrib/httpimg/httpimg.go @@ -5,10 +5,10 @@ import ( "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) { +// Register 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 Register(f *gofpdf.Fpdf, urlStr, tp string) (info *gofpdf.ImageInfoType) { info = f.GetImageInfo(urlStr) if info != nil { -- cgit v1.2.1-24-ge1ad