gpt4 book ai didi

java - PDFbox加载大文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:35 25 4
gpt4 key购买 nike

我正在尝试使用 PDFBox 将 pdf 文件的第一页转换为图像。当我加载大型 pdf 文件时出现异常。

代码:

    PDDocument doc;
try {
InputStream input = new URL("http://www.jewishfederations.org/local_includes/downloads/39497.pdf").openStream();
doc = PDDocument.load(input);
PDPage firstPage = (PDPage) doc.getDocumentCatalog().getAllPages().get(0);
BufferedImage image =firstPage.convertToImage();
File outputfile = new File("image2.png");
ImageIO.write(image, "png", outputfile);
input.close();
doc.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

异常(exception):

org.apache.pdfbox.pdfparser.BaseParser parseCOSStream
WARNING: Specified stream length 72435 is wrong. Fall back to reading stream until 'endstream'.
org.apache.pdfbox.exceptions.WrappedIOException: Could not push back 72435 bytes in order to reparse stream. Try increasing push back buffer using system property org.apache.pdfbox.baseParser.pushBackSize
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSStream(BaseParser.java:554)
at org.apache.pdfbox.pdfparser.PDFParser.parseObject(PDFParser.java:605)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:194)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1219)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1186)
at Worker.main(Worker.java:27)
Caused by: java.io.IOException: Push back buffer is full
at java.io.PushbackInputStream.unread(Unknown Source)
at org.apache.pdfbox.io.PushBackInputStream.unread(PushBackInputStream.java:144)
at org.apache.pdfbox.io.PushBackInputStream.unread(PushBackInputStream.java:133)
at org.apache.pdfbox.pdfparser.BaseParser.parseCOSStream(BaseParser.java:550)
... 5 more

最佳答案

1.8.* PDFBox 版本的替代解决方案是使用非顺序解析器。在那种情况下,代码将不会是

doc = PDDocument.load(input);

但是

doc = PDDocument.loadNonSeq(input, null);

该解析器(将是即将推出的 2.0 版本中唯一的解析器)与回推缓冲区的大小无关。

关于java - PDFbox加载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com