tun Ich habe einen einfacheren Ansatz gemacht, der keine Nachrichten zwischen Iframes mit der gleichen Bibliothek (mit der neuesten Version), using pdf.js.
Das folgende Beispiel würde die ganzen Text nur von der ersten Seite des PDF extrahieren:
/**
* Retrieves the text of a specif page within a PDF Document obtained through pdf.js
*
* @param {Integer} pageNum Specifies the number of the page
* @param {PDFDocument} PDFDocumentInstance The PDF document obtained
**/
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);
});
});
});
}
/**
* Extract the test from the PDF
*/
var PDF_URL = '/path/to/example.pdf';
PDFJS.getDocument(PDF_URL).then(function (PDFDocumentInstance) {
var totalPages = PDFDocumentInstance.pdfInfo.numPages;
var pageNumber = 1;
// Extract the text
getPageText(pageNumber , PDFDocumentInstance).then(function(textPage){
// Show the text of the page in the console
console.log(textPage);
});
}, function (reason) {
// PDF loading error
console.error(reason);
});
Read the article about this solution here. Da @xarxziux erwähnt wurde, hat sich die Bibliothek geändert, seit die erste Lösung veröffentlicht wurde (sie sollte nicht mehr mit der neuesten Version von pdf.js funktionieren). Dies sollte für die meisten Fälle funktionieren.
Vielen Dank, es funktionierte für mich: D – Coccinelle
Hinweis für zukünftige Googler: Das offizielle pdf.js-Projekt scheint mehrmals den Besitzer gewechselt zu haben, da die obigen Links veröffentlicht wurden, aber es befindet sich derzeit in Mozillas GitHub-Seite - https://github.com/mozilla/pdf.js – xarxziux