करना चाहिए मैंने एक आसान दृष्टिकोण बनाया है जिसे एक ही लाइब्रेरी (नवीनतम संस्करण का उपयोग करके), using pdf.js का उपयोग कर आईफ्रेम के बीच संदेश पोस्ट करने की आवश्यकता नहीं है।
निम्न उदाहरण केवल पीडीएफ के पहले पृष्ठ से सभी पाठ निकाल होगा:
/**
* 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। जैसा कि @xarxziux ने उल्लेख किया है, पहला समाधान पोस्ट होने के बाद लाइब्रेरी बदल गई है (इसे अब पीडीएफ.जेएस के नवीनतम संस्करण के साथ काम नहीं करना चाहिए)। यह ज्यादातर मामलों के लिए काम करना चाहिए।
स्रोत
2017-03-05 09:37:34
धन्यवाद, यह मेरे लिए काम करता है: डी – Coccinelle
भावी Googlers के लिए नोट: उपरोक्त लिंक पोस्ट किए जाने के बाद से आधिकारिक पीडीएफ.जेएस प्रोजेक्ट कई बार हाथ बदल गया है, लेकिन यह वर्तमान में मोज़िला के गिटहब पेज में रहता है - https://github.com/mozilla/pdf.js – xarxziux