Solution 1 :

As @haraldK pointed out in the comments, the GIF file provided via the XML API does not conform to the GIF standard and thus cannot be parsed by Java’s ImageIO API.

Since there does not seem to exist a pure Java tool to repair the file, the workaround I came up with now is to use ImageMagick via Java’s Process API. Calling the convert command with the -coalesce option will parse the broken GIF and create a new one that does conform to the GIF standard.

// Decode broken GIF image and write to disk
final String base64Gif = "[Base64 as provided by API]";
final byte[] sigImg = Base64.decodeBase64(base64Gif);
Path gifPath = Paths.get("C:/Temp/pod_1Z12345E5991872040.tmp.gif");
if (!Files.exists(gifPath)) {
Files.write(gifPath, sigImg, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);

// Use the Java Process API to call ImageMagick (on Linux you would use the 'convert' binary)
ProcessBuilder procBuild = new ProcessBuilder();
procBuild.command("C:\Program Files\ImageMagick-7.0.9-Q16\magick.exe", "C:\Temp\pod_1Z12345E5991872040.tmp.gif", "-coalesce", "C:\Temp\pod_1Z12345E5991872040.gif");
Process proc = procBuild.start();

// Wait for ImageMagick to complete its work

The newly created file can be read by Java’s ImageIO API and be used as expected.

Problem :

I got a strange issue with a GIF image in Java. The image is provided by an XML API as Base64 encoded string. To decode the Base64, I use the commons-codec library in version 1.13.

When I just decode the Base64 string and write the bytes out to a file, the image shows properly in browsers and MS Paint (nothing else to test here).

final String base64Gif = "[Base64 as provided by API]";
final byte[] sigImg = Base64.decodeBase64(base64Gif);
File sigGif = new File("C:/Temp/");
try (FileOutputStream fos = new FileOutputStream()) {

The resulting file opened in MS Paint:

The resulting file opened in MS Paint

But when I now start consuming this file using Java (for example creating a PDF document from HTML using the openhtmltopdf library), it is corrupted and does not show properly.

final String htmlLetterStr = "[HTML as provided by API]";
final Document doc = Jsoup.parse(htmlLetterStr);

try (FileOutputStream fos = new FileOutputStream(new File("C:/Temp/letter_1Z12345E5991872040.pdf"))) {
    PdfRendererBuilder builder = new PdfRendererBuilder();

    builder.withW3cDocument(new W3CDom().fromJsoup(doc), "file:///C:/Temp/");
    builder.useDefaultPageSize(210, 297, BaseRendererBuilder.PageSizeUnits.MM);;


When I now open the resulting PDF, the image created above looks like this. It seems that only the first pixel lines are printed, some layer is missing, or something like that.

enter image description here

The same happens, if I read the image again with ImageIO and try to convert it into PNG. The resulting PNG looks exactly the same as the image printed in the PDF document.

How can I get the image to display properly in the PDF document?

Link to original GIF Base64 as provided by API:


Comment posted by Joop Eggen

Was it like this:

Comment posted by Harald K

Can you share the original GIF?

Comment posted by marandus

@haraldK I added a link to the Base64 string provided by the API

Comment posted by marandus

@JoopEggen no, it is no HTML API, it is an XML API. So it is provided as

Comment posted by Harald K

TLDR; Your “GIF” is broken. By broken, I mean not according to spec, even if some decoders can read it. If I add a warning listener while reading, I get a lot “Out-of-sequence code” warnings, which is probably from the LZW decoding (I didn’t debug further). You may have luck using other software/libraries, but the built-in ImageIO GIF plugin won’t read it.