繁体   English   中英

使用PDFBox合并大型PDF文件时出错-文件标记'%% EOF'丢失结尾

[英]Error Merging Large PDF Files with PDFBox - Missing end of file marker '%%EOF'

我已经使用InputStreams使用PDFBox成功实现了pdf合并解决方案。 但是,当我尝试合并非常大的文档时,出现以下错误:

Caused by: java.io.IOException: Missing root object specification in trailer.
at org.apache.pdfbox.pdfparser.COSParser.parseTrailerValuesDynamically(COSParser.java:2832) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:173) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:220) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1144) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1060) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.multipdf.PDFMergerUtility.legacyMergeDocuments(PDFMergerUtility.java:379) ~[pdfbox-2.0.11.jar:2.0.11]
at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:280) ~[pdfbox-2.0.11.jar:2.0.11]

我认为,更重要的是在错误之前发生的这些语句:

FINE (pdfparser.COSParser) [] - Missing end of file marker '%%EOF'
FINE (pdfparser.COSParser) [] - Set missing offset 388 for object 2 0 R

在我看来,它在非常大的文件中找不到'%%EOF'标记。 现在我知道它确实存在,因为我可以查看源代码(不幸的是我无法提供文件本身)。

在网上进行一些搜索后,我发现COSParser类上有一个setEOFLookupRange()方法。 我想知道查询范围是否太小,这就是为什么它找不到'%%EOF'标记的原因。 问题是...我的代码中根本没有使用COSParser对象。 我只使用PDFMergerUtility类。 PDFMergerUtility似乎在COSParser使用COSParser

所以我的问题是

  1. 我对EOFLookupRange假设正确吗?
  2. 如果是这样,如何设置我的代码中仅包含PDFMergerUtility而不包含COSParser对象的范围?

非常感谢您的宝贵时间!

用下面的代码更新

 private boolean getCoolDocuments(final String slateId, final String filePathAndName)
            throws IOException {

        boolean status = false;
        InputStream pdfStream = null;
        HttpURLConnection connection = null;
        final PDFMergerUtility merger = new PDFMergerUtility();
        final ByteArrayOutputStream mergedPdfOutputStream = new ByteArrayOutputStream();

        try {

            final List<SlateDocument> parsedSlateDocuments = this.getSpecificDocumentsFromSlate(slateId);

            if (!parsedSlateDocuments.isEmpty()) {

                // iterate through each document, adding each pdf stream to the merger utility
                int numberOfDocuments = 0;
                for (final SlateDocument slateDocument : parsedSlateDocuments) {

                    final String url = this.getBaseURL() + "/slate/" + slateId + "/documents/"
                            + slateDocument.getDocumentId();

                     /* code for RequestResponseUtil.initializeRequest(...) below */
                    connection = RequestResponseUtil.initializeRequest(url, "GET", this.getAuthenticationHeader(),
                            true, MediaType.APPLICATION_PDF_VALUE);

                    if (RequestResponseUtil.isSuccessful(connection.getResponseCode())) {
                        pdfStream = connection.getInputStream();

                    }
                    else {
                        /* do various things */
                    }

                    merger.addSource(pdfStream);
                    numberOfDocuments++;
                }

                merger.setDestinationStream(mergedPdfOutputStream);

                // merge the all the pdf streams together
               merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());

               status = true;
            }
            else {
                LOG.severe("An error occurred while parsing the slated documents; no documents remain after parsing!");
            }
        }
        finally {
            RequestResponseUtil.close(pdfStream);

            this.disconnect(connection);
        }

        return status;
    }

   public static HttpURLConnection initializeRequest(final String url, final String method,
            final String httpAuthHeader, final boolean multiPartFormData, final String reponseType) {

    HttpURLConnection conn = null;

    try {
        conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod(method);
        conn.setRequestProperty("X-Slater-Authentication", httpAuthHeader);
        conn.setRequestProperty("Accept", reponseType);
        if (multiPartFormData) {
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=BOUNDARY");
            conn.setDoOutput(true);
        }
        else {
            conn.setRequestProperty("Content-Type", "application/xml");
        }
    }
    catch (final MalformedURLException e) {
        throw new CustomException(e);
    }
    catch (final IOException e) {
        throw new CustomException(e);
    }
    return conn;

}

我怀疑这是InputStream的问题。 这并不是我真正想的,但基本上我是在(非常错误)的假设下做出这样的假设:

           pdfStream = connection.getInputStream();
                /* ... */
           merger.addSource(pdfStream);

当然,这将无法正常工作,因为可能会读取或可能不会读取整个InputStream 需要显式读取它,直到到达最后一个-1字节为止。 我很确定在较小的文件上它可以正常工作,并且实际上可以在整个流中读取,但是在较大的文件上,它根本没有达到目的...因此找不到%%EOF标记。

解决方案是使用中间的ByteArrayOutputStream ,然后通过ByteArrayInputStream将其转换回InputStream

因此,如果您替换以下代码行:

pdfStream = connection.getInputStream();

上面的代码:

                final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                int c;
                while ((c = connection.getInputStream().read()) != -1) {
                    byteArrayOutputStream.write(c);
                }

                pdfStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

您将得到一个可行的示例。

我可能最终会将其更改为实现,以改为使用Pipes或Circular Buffers ,但是至少目前为止这是可行的。

尽管这不一定是Java 101错误,但它更像是Java 102错误,仍然很可耻。 :/希望它会帮助别人。

感谢@Tilman Hausherr和@Master_ex提供的所有帮助!

我看了一下代码,发现EOFLookupRange中的默认COSParser2048字节

我认为您的假设是正确的。

展望PDFParser延伸的COSParser ,是由内部使用的解析器PDFMergerUtility我看到它,可以设置其他EOFLookupRange通过使用系统属性 系统属性名称是org.apache.pdfbox.pdfparser.nonSequentialPDFParser.eofLookupRange ,它应该是有效的整数。

是一个演示如何设置系统属性的问题。

我没有测试以上内容,但我希望它能起作用:)

PDFBox代码的链接使用的是2.0.11版本。

暂无
暂无

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

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