Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox IOException: End of File, expected line

I am currently trying to grab text from a PDF that is already uploaded and accessed through a link by using PDFBox and Selenium. I used this as a source: 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)

Why am I getting this error?

like image 623
stevek Avatar asked Jun 20 '18 17:06

stevek


2 Answers

Here is the example that you asked to share using PDFURL

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
}

For using PDF as local file, URL and BufferedInputStream needs to be replaced by

 File file = new File(pdf_url);
 PDFParser parser = new PDFParser(new FileInputStream(file));

Hope this helps

like image 72
Prany Avatar answered Nov 10 '22 01:11

Prany


Please check either files are with size of 0 KB or You may check with try (final PDDocument document = PDDocument.load(file, MemoryUsageSetting.setupTempFileOnly())){

like image 34
Shahid Hussain Abbasi Avatar answered Nov 09 '22 23:11

Shahid Hussain Abbasi