Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Premature EOF while reading JPG exception while writing jpg images to pdf using itext

Tags:

java

jpeg

itext

I am trying to insert jpg image into the PDF. Some jpg images works properly but in some cases I get following exception.

java.io.IOException: Premature EOF while reading JPG.
    at com.itextpdf.text.Jpeg.processParameters(Jpeg.java:218)
    at com.itextpdf.text.Jpeg.<init>(Jpeg.java:117)
    at com.itextpdf.text.Image.getInstance(Image.java:279)
    at com.itextpdf.text.Image.getInstance(Image.java:241)
    at com.itextpdf.text.Image.getInstance(Image.java:364)

Below is the code I am using.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImagesNextToEachOther {

    public static final String DEST = "/home/Documents/pdftest/hello.pdf";

    public static final String IMG1 = "/home/Documents/pdftest/2.jpg";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImagesNextToEachOther().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);
        table.addCell(createImageCell(IMG1));
        document.add(table);
        document.close();
    }

    public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);
        return cell;
    }
}

I am getting error on the following line in the above code.

Image img = Image.getInstance(path);

path is the full path of the image.

I found similar question on SO

Premature EOF while reading JPG using itext

Failure to read JPEG file from byte[]

But this did not solve my issue.

Here's a link to one of such image

https://dl.dropboxusercontent.com/u/46349359/image.jpg

like image 268
ashishjmeshram Avatar asked Jun 22 '17 03:06

ashishjmeshram


People also ask

What does this error message mean when reading JPGs?

"Error encountered while reading JPEG image. Image may be damaged or incompatible. Resave the image with different settings and try again."someone I get this message when I try to place jpgs into indesign.

What is iText and how to read a PDF file?

In this iText tutorial, we are writing various code examples to read a PDF file and write a PDF file. iText library helps in dynamically generating the .pdf files from Java applications. The given code examples are categorized into multiple sections based on the functionality they achieve.

What is the premature EOF exception?

The premature EOF exception generally occurs in situations where a connection to a particular application server connection was lost while the XCC driver was in the process of reading a result set.

How to fix the EOF error in GIMP?

You can check this for yourself by opening the image in the GIMP and then select File > Overwrite image.jpg, the GIMP will fix the image, and the EOF error will disappear. If you download this image, and you use it with your code, the error will not occur.


1 Answers

As Amedee already explained in his comments, the JPG is broken. You can check this for yourself by opening the image in the GIMP and then select File > Overwrite image.jpg, the GIMP will fix the image, and the EOF error will disappear.

I have done this for you, and the result is:

enter image description here

If you download this image, and you use it with your code, the error will not occur.

How does that help me? you might ask. I can see the image in a browser. I can see the image in an image viewer. Why don't you solve that problem in iText?

The answer is simple: JPG is natively supported by PDF, which means that we can put an exact copy of all the JPG image bytes inside the PDF. However, before we do so, iText performs a sanity check on the image. When this sanity check fails, iText will (and should) reject the image, because there is a high chance that a PDF containing such a "broken" image will show an error message if we'd use it.

Image viewers or image editing tools (such as the GIMP), are more tolerant. They ignore the fact that the image isn't well-formed. In the case of the GIMP, the tool fixes the errors, and gives you to opportunity to "overwrite" the image to store the fixes.

There are currently no plans to have iText perform such fixes. We have provided such a fix for broken TIFF files, but even then, the default is to reject broken images. If you want iText to fix a broken TIFF file, you have to set a flag because most of our customers prefer getting an exception than taking the risk of adding an image that is automatically fixed. If you are an iText customer, feel free to post a support request to have similar "broken image fixing" functionality to iText; if you are not an iText customer, feel free to add this fix yourself, and publish that fix under the AGPL along with the rest of the code of your project (as you know, iText's AGPL makes it mandatory for you to publish the full source code of your project in most cases).

like image 53
Bruno Lowagie Avatar answered Sep 20 '22 08:09

Bruno Lowagie