ich würde gerne ein Script schreiben, dass Text aus einer pdf liest. Da Java keine pdfs ohne externe Library lesen kann(ich weiß zumindest nicht wie) muss ich die pdf mithilfe von JavaScript lesen. Ich hatte vor pdf.js zu nutzen. Leider bekomme ich immer den Fehler: Warning: Setting up fake worker.
der Konsolenoutput:
Der Code funktioniert normal in html/js(natürlich ohne load Befehl).
Geht das überhaupt in Nashorn?
Java:
var JavaPackages = new JavaImporter(java.io,
java.lang,
java.io,
java.util,
java.nio.file,
java.nio.charset.StandardCharsets,
);
with(JavaPackages) {
logger.info("start");
try {
load('C:/Users/AltmannT/Downloads/pdfjs-2.5.207-es5-dist/build/pdf.js');
logger.info("loaded");
var url = 'C:/Users/AltmannT/Documents/file.pdf';
pdfjsLib.GlobalWorkerOptions.workerSrc = 'C:/Users/AltmannT/Downloads/pdfjs-2.5.207-es5-dist/build/pdf.worker.js';
logger.info("set worker");
var loadingTask = pdfjsLib.getDocument(url);
logger.info(loadingTask.docId);
loadingTask.promise.then(function (PDFDocumentInstance) {
logger.info("bin in der function");
var totalPages = PDFDocumentInstance.numPages;
var pageNumber = 1;
// Extract the text
getPageText(pageNumber , PDFDocumentInstance).then(function(textPage){
// Show the text of the page in the console
logger.info(textPage);
});
}, function (reason) {
// PDF loading error
logger.info("error loading pdf"+reason);
});
function getPageText(pageNum, PDFDocumentInstance) {
// Return a Promise that is solved once the text of the page is retrieven
return new Promise(function (resolve, reject) {
PDFDocumentInstance.getPage(pageNum).then(function (pdfPage) {
// The main trick to obtain the text of the PDF page, use the getTextContent method
pdfPage.getTextContent().then(function (textContent) {
var textItems = textContent.items;
var finalString = "";
// Concatenate the string of the item to the final string
for (var i = 0; i < textItems.length; i++) {
var item = textItems[i];
finalString += item.str + " ";
}
// Solve promise with the text retrieven from the page
resolve(finalString);
});
});
});
}
} catch (e) {
var sw = new StringWriter();
var pw = new PrintWriter(sw);
e.printStackTrace(pw)
logger.info("Fehler: " + e+sw.toString());
}
logger.info("end");
}
Java:
INFO root - start
INFO root - geloaded
INFO root - set worker
INFO root - Warning: Setting up fake worker.
INFO root - d0
INFO root - end
Geht das überhaupt in Nashorn?