jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

addImage - GIF with transparency renders with green background

Open stoyan-karev opened this issue 4 years ago • 7 comments

Hello, First of all, let me thank you for the awesome library!

I am currently using version 1.5.3 in a project and I encountered an issue when creating a PDF with some GIF images that have a transparent background. They are added successfully in the resulting document but are rendered with a green background.

image

It seems that the behavior is specific to GIF images. PNG images with transparency are rendered correctly. I have created a small example that generates a PDF with an image in PNG and GIF format to demonstrate the difference. Please find it below https://codepen.io/lazy-panda/pen/LQEEQm

Unfortunately, in my particular case, I don't have control over the image format. Is it possible to take the GIF's transparency when adding it to the PDF?

Any help is greatly appreciated.

stoyan-karev avatar Mar 27 '20 15:03 stoyan-karev

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

github-actions[bot] avatar Jul 03 '20 01:07 github-actions[bot]

This problem occurs within jsPDFAPI.processGIF89A, either GifReader() or JPEGEncoder.encode(). Most likely is GifReader(). The data passed to jsPDFAPI.processJPEG.call has a green background already.

kakugiki avatar Jul 11 '20 11:07 kakugiki

Okay, it's this.decodeAndBlitFrameBGRA = function (frame_num, pixels) of GifReader(), line 662 of omggif.js. I don't know why the background is green, but I can change op += 4; and force the background white with the following code.

pixels[op++] = 255; pixels[op++] = 255; pixels[op++] = 255; pixels[op++] = 255;

kakugiki avatar Jul 24 '20 03:07 kakugiki

Mmh if I am right, op += 4 sets all for 4 channels of this pixel to 0. And alpha=0 means this pixel is transparent, so the code in omggif seems to be right. I rather think the issue is the JPEGEncoder, because JPEG does not support transparency. You can fake transparency in JPEG by assigning the transparent pixels a certain color that is interpreted as transparent by the viewer (here green). For PDF, white is probably the better color, I agree, although this still is not really transparent and won't work if the image lies on top of other objects or is drawn onto a background with different color. Maybe we can check how the png_support achieves transparency. AFAIK it uses transparency masks. Maybe we can convert it to a PNG instead of a JPEG?

HackbrettXXX avatar Jul 24 '20 10:07 HackbrettXXX

Yeah, I thought it can't be that simple. Thank you for the explanation. I thought about converting to PNG, but it's quite different and complicated. I'll take a second look.

kakugiki avatar Jul 24 '20 12:07 kakugiki

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

github-actions[bot] avatar Oct 24 '20 01:10 github-actions[bot]

This bug still exists. Is there any chance it will be fixed soon?

By the way, why are GIFs added as JPEGs? PNG should be a better option, isn't it?

3bl3gamer avatar Oct 17 '22 15:10 3bl3gamer