如何使用硒读取pdf文件 [英] How to read the pdf file using selenium
问题描述
我正在一个有链接的网页上工作,单击它会在新窗口上打开一个pdf文件.我必须阅读该pdf文件,以对照完成的交易验证一些数据.一种方法是下载该文件,然后使用它.谁能帮我这个忙.我必须在IE 11上工作
I am working on web page over which there is a link, clicking on which it opens a pdf file on new window. I have to read that pdf file to validate some data against the transactions done. One way is to download that file and then use it. Can any one help me out on this. I have to work on IE 11
在此先感谢.
推荐答案
使用PDFBox和FontBox.
Use PDFBox and FontBox.
public String readPDFInURL() throws EmptyFileException, IOException {
WebDriver driver = new FirefoxDriver();
// page with example pdf document
driver.get("file:///C:/Users/admin/Downloads/dotnet_TheRaceforEmpires.pdf");
URL url = new URL(driver.getCurrentUrl());
InputStream is = url.openStream();
BufferedInputStream fileToParse = new BufferedInputStream(is);
PDDocument document = null;
try {
document = PDDocument.load(fileToParse);
String output = new PDFTextStripper().getText(document);
} finally {
if (document != null) {
document.close();
}
fileToParse.close();
is.close();
}
return output;
}
由于不赞成使用旧版本的PDFBox中的某些功能,因此我们需要将另一个FontBox与PDFBox一起使用.我使用了 PDFBox(2.0.3)和
Since some of the functions from the older versions of PDFBox have been deprecated, we need to use another FontBox along with PDFBox. I have used PDFBox (2.0.3) and FontBox (2.0.3) and it is working fine. It won't read images though.
这篇关于如何使用硒读取pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!