jsPDF
jsPDF copied to clipboard
addImage - GIF with transparency renders with green background
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.
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.
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.
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.
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;
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?
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.
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.
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?