signintech / gopdf

A simple library for generating PDF written in Go lang
MIT License
2.57k stars 277 forks source link

Support Image(s) From Readers #214

Open jenpet opened 2 years ago

jenpet commented 2 years ago

Hi there,

I've fiddled around with a lot of libs but this is so far the most promising one to me, so thanks for the effort you guys put into it. I'm currently facing an issue which either is caused by a misuse of the library within my code or by some missing functionality within the library itself.

Scenario:

I have base64 encoded images that I process within my application which are not available on disk as a binary file. The image format is a PNG and I need to add them into my generated PDFs. There are several approaches to this but the most obvious one does unfortunately not work for me.

Approach 1: using ImageFrom()

Decode the image and pass an image.Image variable into the lib's ImageFrom() function.

// setup foo
s := "<BASE64_ENCODED_IMG>"
r := base64.NewDecoder(base64.StdEncoding, strings.NewReader(s))
img, _, _ := image.Decode(r)
pdf.ImageFrom(img, 0,0, nil)

This results in a completely black box having the correct measurements but obviously not satisfying my actual need. Checking the ImageFrom() function puzzles me a bit since it enforces JPEG encoding independent of the actual image input.

    // ...
    go func() {
        bw := bufio.NewWriter(w)
        err := jpeg.Encode(bw, img, nil)
        bw.Flush()
        if err != nil {
            w.CloseWithError(err)
        } else {
            w.Close()
        }
    }()
    // ...

Approach 2: write to and again read from disk using Image()

This possibility is unfortunately not a real option since I do not want to invest into housekeeping of images which are only temporarily written to the disk, re-read into the memory and then get deleted again. Nevertheless, it actually works since it utilizes different image processing logic.

Possible Mitigation(s)

Let me guys know what you think about it or give me a little push in the right direction in case I'm somehow lost.

Regards & thanks

ixuer commented 2 years ago

In fact, there is no restriction to use .jpeg format images. I used go-chart to generate the chart, then converted it to bytes, and then used ImageHolderByBytes(). If the image content cannot be read, consider whether it is because image.Decode() has finished reading the data stream.

jenpet commented 2 years ago

Hey @ixuer thanks for your reply. Will check that during the next days and maybe come back with a snippet that either works or doesn't :-D Appreciate your answer!

vantaboard commented 4 months ago

@oneplus1000 can you close this issue if it has been resolved? It seems to have been resolved.