gpt4 book ai didi

java - 使用 pdfbox 2.0.8 时 block 类型无效

转载 作者:行者123 更新时间:2023-12-01 20:05:19 26 4
gpt4 key购买 nike

我正在尝试使用 PDFBox 从 PDF 中获取文本,但遇到了异常。这是我用来进行文本剥离的代码。

  try
{
PDDocument document = PDDocument.load(decodedPdfDocument.getBytes());
PDFTextStripper Tstripper = new PDFTextStripper();
String st = Tstripper.getText(document);
System.out.println("Text:" + st);
}
catch (Exception e)
{
e.printStackTrace();
}

这是我遇到的异常。

java.io.IOException: java.util.zip.DataFormatException: invalid block type
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:83)
at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:69)
at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:167)
at org.apache.pdfbox.pdmodel.PDPage.getContents(PDPage.java:155)
at org.apache.pdfbox.pdfparser.PDFStreamParser.<init>(PDFStreamParser.java:91)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:485)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:469)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150)
at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139)
at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391)
at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319)
at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266)
at org.apache.pdfbox.text.PDFTextStripper.getText(PDFTextStripper.java:227)
at com.cerner.fsi.testing.xds.provideandregister.ClinicalEventInErrorTest.testProvideAndRegisterCDAWrappedDocument_ClinicalEventInError(ClinicalEventInErrorTest.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at com.cerner.fsi.testing.xds.EnterpriseClientSpringJUnit4ClassRunner$1$1.run(EnterpriseClientSpringJUnit4ClassRunner.java:50)
at com.cerner.fsi.testing.xds.EnterpriseClientSpringJUnit4ClassRunner$1$1.run(EnterpriseClientSpringJUnit4ClassRunner.java:1)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.cerner.fsi.testing.xds.EnterpriseClientSpringJUnit4ClassRunner$1.evaluate(EnterpriseClientSpringJUnit4ClassRunner.java:43)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.zip.DataFormatException: invalid block type
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:259)
at java.util.zip.Inflater.inflate(Inflater.java:280)
at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:108)
at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:74)
... 49 more

我已通过验证运行该 PDF,它是有效的 1.4 PDF。我还可以使用 Foxit 和 Acrobat 前端工具提取文本。不太确定我在这里做错了什么,这是我第一次使用这个 API。

最佳答案

根据OP的评论

The decodedPdfDocument comes from decoding a Base64 string. I'm creating a new string using new String(Base64.decode(documentString), "UTF-8");

这是错误。切勿将二进制内容(如 PDF)视为文本!相反,让解码后的 PDF 文档保留为 byte[]:

documentString = [...your base64 encoded PDF ...];
byte[] decodedPdfDocument = Base64.decode(documentString);
...
PDDocument document = PDDocument.load(decodedPdfDocument);
PDFTextStripper textStripper = new PDFTextStripper();
String st = textStripper.getText(document);
<小时/>

作为背景:当你做类似的事情

String string = new String(bytes, encoding);

对于byte[]字节,您的字节会被解码,假设它们包含使用编码编码为字节的文本。

如果您的字节不是使用encoding进行文本编码的,此过程可能具有破坏性,根据假定的编码没有意义的字节序列将被转换为String 中的替换字符。当您稍后使用 getBytes() 从字符串中检索字节时,您将检索替换字符的表示形式,而不是原始字节序列。

关于java - 使用 pdfbox 2.0.8 时 block 类型无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47515609/

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