2013-07-02 5 views
9

का उपयोग कर पीडीएफ फ़ाइल से पाठ निकालें मैं सर्वर का उपयोग किये बिना क्लाइंट पक्ष में केवल जावास्क्रिप्ट का उपयोग करके पीडीएफ फ़ाइल से टेक्स्ट निकालना चाहता हूं। extract text from pdf in Javascriptजावास्क्रिप्ट

और फिर

http://hublog.hubmed.org/archives/001948.html

में और में: पता है कि कृपया

https://github.com/hubgit/hubgit.github.com/tree/master/2011/11/pdftotext

1) मैं चाहता हूँ मैं पहले से ही नीचे दिए गए लिंक में एक जावास्क्रिप्ट कोड मिल गया है वे फ़ाइलें हैं जो पिछले निष्कर्षों से इन निष्कर्षणों के लिए जरूरी हैं। 2) मुझे नहीं पता कि इन कोडों को किसी एप्लिकेशन में कैसे अनुकूलित किया जाए, वेब पर नहीं।

कोई भी जवाब स्वागत है। धन्यवाद।

उत्तर

8

यहाँ कैसे पाठ निकालने के लिए उपयोग करने के लिए pdf.js का एक अच्छा उदाहरण है: http://git.macropus.org/2011/11/pdftotext/example/

निश्चित रूप से

आप अपने उद्देश्य के लिए कोड का एक बहुत दूर करने के लिए है, लेकिन यह यह

+0

धन्यवाद, यह मेरे लिए काम करता है: डी – Coccinelle

+1

भावी Googlers के लिए नोट: उपरोक्त लिंक पोस्ट किए जाने के बाद से आधिकारिक पीडीएफ.जेएस प्रोजेक्ट कई बार हाथ बदल गया है, लेकिन यह वर्तमान में मोज़िला के गिटहब पेज में रहता है - https://github.com/mozilla/pdf.js – xarxziux

1

करना चाहिए मैंने एक आसान दृष्टिकोण बनाया है जिसे एक ही लाइब्रेरी (नवीनतम संस्करण का उपयोग करके), 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 ने उल्लेख किया है, पहला समाधान पोस्ट होने के बाद लाइब्रेरी बदल गई है (इसे अब पीडीएफ.जेएस के नवीनतम संस्करण के साथ काम नहीं करना चाहिए)। यह ज्यादातर मामलों के लिए काम करना चाहिए।

संबंधित मुद्दे