I was trying to read PDF contents which is opened in new tab using Protractor, but could not find any relevant npm library that can provide openConnection() method unlike Java,
Below is the code for Java that works in selenium. Any help if anyone knows similar methods that we can use to read PDF contents in protractor please?
public static String readPDFContent(String pdfURL) throws Exception {
URL url = new URL(pdfURL);
URLConnection conn = url.openConnection();
conn.connect();
InputStream input = conn.getInputStream();
BufferedInputStream fileToParse = new BufferedInputStream(input);
PDDocument document = null;
String output = null;
try {
document = PDDocument.load(fileToParse);
output = new PDFTextStripper().getText(document);
System.out.println(output);
} finally {
if (document != null) {
document.close();
}
fileToParse.close();
input.close();
}
return output;}
May be, if anyone knows how to open an URLconnections using JavaScript or TypeScript or how to use Java classes in scripts, that would also help.
I managed to write below code but it is not working, as the PDF file is actually an attachment to the response. And I found similar thread NodeJS Read file attachment from HTTP response which is unanswered.
static readPDFContent(URLValue: string, cookie: string) { let header = { 'Content-Type': 'application/pdf', 'Accept': '/', 'Connection': 'keep-alive', 'Cookie': cookie }; let options = { url: URLValue, method: 'GET', headers: header };
let request = http.request(options, function (response) {
console.log("Request is => " + request);
let output = '';
response.on('data', function (chunk) {
output += chunk;
});
response.on('end', function () {
console.log("Output is => " +output);
});
});
request.on('error', function (err) {
console.log(err.message);
});
request.end();