PDFBox IOException:文件结尾,预期行
PDFBox IOException: End of File, expected line
我目前正在尝试使用 PDFBox 和 Selenium 从已上传并通过 link 访问的 PDF 中获取文本。
我用这个作为来源:http://www.seleniumeasy.com/selenium-tutorials/how-to-extract-pdf-text-and-verify-using-selenium-webdriver-java
public String function(String pdf_url) {
PDFTextStripper pdfStripper = null;
PDDocument pDoc;
COSDocument cDoc;
String parsedText = "";
try {
URL url = new URL(pdf_url);
BufferedInputStream file = new BufferedInputStream(url.openStream());
PDFParser parser = new PDFParser(file);
parser.parse();
cDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(1);
pDoc = new PDDocument(cDoc);
parsedText = pdfStripper.getText(pDoc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parsedText;
}
Error: End-of-File expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1519)
at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:372)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:186)
at scripts.Script.grabPDF_Text(Script.java:94)
at scripts.Script.main(Script.java:817)
为什么会出现此错误?
这是您要求使用 PDF 共享的示例URL
string PDFURL = "https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf";
function(PDFURL1);
public String function(String pdf_url)
{
//Exact same code as yours
}
使用PDF作为本地文件,URL和BufferedInputStream需要替换为
File file = new File(pdf_url);
PDFParser parser = new PDFParser(new FileInputStream(file));
希望对您有所帮助
请检查其中一个文件的大小是否为 0 KB
要么
您可以尝试检查 (final PDDocument document = PDDocument.load(file, MemoryUsageSetting.setupTempFileOnly())){
我目前正在尝试使用 PDFBox 和 Selenium 从已上传并通过 link 访问的 PDF 中获取文本。 我用这个作为来源:http://www.seleniumeasy.com/selenium-tutorials/how-to-extract-pdf-text-and-verify-using-selenium-webdriver-java
public String function(String pdf_url) {
PDFTextStripper pdfStripper = null;
PDDocument pDoc;
COSDocument cDoc;
String parsedText = "";
try {
URL url = new URL(pdf_url);
BufferedInputStream file = new BufferedInputStream(url.openStream());
PDFParser parser = new PDFParser(file);
parser.parse();
cDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(1);
pDoc = new PDDocument(cDoc);
parsedText = pdfStripper.getText(pDoc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parsedText;
}
Error: End-of-File expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1519)
at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:372)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:186)
at scripts.Script.grabPDF_Text(Script.java:94)
at scripts.Script.main(Script.java:817)
为什么会出现此错误?
这是您要求使用 PDF 共享的示例URL
string PDFURL = "https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf";
function(PDFURL1);
public String function(String pdf_url)
{
//Exact same code as yours
}
使用PDF作为本地文件,URL和BufferedInputStream需要替换为
File file = new File(pdf_url);
PDFParser parser = new PDFParser(new FileInputStream(file));
希望对您有所帮助
请检查其中一个文件的大小是否为 0 KB 要么 您可以尝试检查 (final PDDocument document = PDDocument.load(file, MemoryUsageSetting.setupTempFileOnly())){