简体   繁体   English

使用itext将jpg图像写入pdf时读取JPG异常时的过早EOF

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

I am trying to insert jpg image into the PDF. 我试图将jpg图像插入PDF。 Some jpg images works properly but in some cases I get following exception. 一些jpg图像正常工作但在某些情况下我得到以下异常。

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. path是图像的完整路径。

I found similar question on SO 我在SO上发现了类似的问题

Premature EOF while reading JPG using itext 使用itext读取JPG时过早的EOF

Failure to read JPEG file from byte[] 无法从byte []读取JPEG文件

But this did not solve my issue. 但这并没有解决我的问题。

Here's a link to one of such image 这是一个这样的图像的链接

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

As Amedee already explained in his comments, the JPG is broken. 正如Amedee在他的评论中已经解释过的那样,JPG被打破了。 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. 你可以通过在GIMP中打开图像然后选择File > Overwrite image.jpg来检查这个,GIMP将修复图像,EOF错误将消失。

I have done this for you, and the result is: 我为你做了这个,结果是:

在此输入图像描述

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? 你为什么不在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. 答案很简单:PDF本身支持JPG,这意味着我们可以在PDF中放置所有JPG图像字节的精确副本。 However, before we do so, iText performs a sanity check on the image. 但是,在我们这样做之前,iText会对图像进行健全性检查。 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. 当这种健全性检查失败时,iText将(并且应该)拒绝该图像,因为如果我们使用它,包含这样的“损坏”图像的PDF很可能会显示错误消息。

Image viewers or image editing tools (such as the GIMP), are more tolerant. 图像查看器或图像编辑工具(例如GIMP)更加宽容。 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. 对于GIMP,该工具可修复错误,并使您有机会“覆盖”图像以存储修复程序。

There are currently no plans to have iText perform such fixes. 目前还没有计划让iText执行此类修复。 We have provided such a fix for broken TIFF files, but even then, the default is to reject broken images. 我们已经为破坏的TIFF文件提供了这样的修复,但即使这样,默认情况下也是拒绝损坏的图像。 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. 如果您希望iText修复损坏的TIFF文件,您必须设置一个标记,因为我们的大多数客户更愿意获得异常,而不是冒着添加自动修复的图像的风险。 If you are an iText customer, feel free to post a support request to have similar "broken image fixing" functionality to iText; 如果您是iText客户,请随时发布支持请求,以便在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). 如果您不是 iText客户,请随意添加此修复程序,并在AGPL下发布该修复程序以及项目的其余代码(如您所知,iText的AGPL强制要求您发布完整的源代码大多数情况下你的项目代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM