jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.34k stars 787 forks source link

How to add image with type []bytes to pdf? #163

Closed mziia closed 6 years ago

mziia commented 6 years ago

Hello again I generate image and I don't want to save it on my hard drive, so I converte it to []byte type. How can I add it to my pdf?

jung-kurt commented 6 years ago

Use RegisterImageOptionsReader() to do this.

If your bytes are already in a buffer of type bytes.Buffer, you can use the buffer's address as the io.Reader. Otherwise, you should use bytes.NewReader().

See the RegisterImageReader() example for details.

mziia commented 6 years ago

Thanks a lot for library and for answers!

Laurent-ALLO commented 5 years ago

Hi,

I try to use this method to embed some image but i doing something wrong i think.

1° First step i encode my image with base64 : imgdata, err := ioutil.ReadFile("signature.png") if err != nil { panic(err) } Base64Image := base64.StdEncoding.EncodeToString(imgdata) 2° Second step, i decode my image and try to put in into pdf : reader := strings.NewReader(base64.StdEncoding.EncodeToString(Base64Image)) infoSignature := pdf.RegisterImageOptionsReader("image_signature",gofpdf.ImageOptions{ImageType:"PNG"},reader) pdf.Image("image_signature",pagew-mright-60,pdf.GetY()+5,38, 21, false, "", 0,"") but it's not working, i think à doing something bad but i don't know what.

Laurent.

jung-kurt commented 5 years ago

You want DecodeString() rather than that second call to EncodeToString(). Be sure to check the returned error code.

Laurent-ALLO commented 5 years ago

Hi,

Thank for fast answer, i make the change and check the error code all it's ok (i store encode data into file) an after load it (with ioutil ReaderFile ). But i have an error : "erreur : interlacing not supported in PNG buffer 0x7f9f10"

My new code for the pdf part (where 'signatureData' is result of DecodeString : reader := bytes.NewReader(signatureData) infoSignature := pdf.RegisterImageOptionsReader("encode_signature.png",gofpdf.ImageOptions{ImageType:"png"},reader) pdf.Image("encode_signature.png",pagew-mright-60,pdf.GetY()+5,38, 21, false, "", 0,"")

jung-kurt commented 5 years ago

It looks like your PNG image is interlaced, that is, encoded so that an unresolved image appears very quickly and then, over seven pases, becomes more highly resolved. It looks like the Go standard library supports Adam7 interlacing but for some reason gofpdf does not. (This could be because the PHP code on which gofpdf is based was copied verbatim, or maybe the Go standard library did not support interlacing when gofpdf was initially written.)

You could try tweaking the code where the png file is parsed (parsepngstream) to accept 0 or 1 on this line to see if that fixes the problem. Alternatively, you could use an image program to change your file to a non-interlaced image.