簡體   English   中英

node.js 中的本地 PDF 文件抓取

[英]local PDF file scraping in node.js

我已經使用 fs 通過 MEAN 堆棧 Web 應用程序上傳了 pdf。 我想從 pdf 中提取某些字段並將它們顯示在網絡應用程序上。 我看過幾個 npm 包,比如 pdf.js、pdf2json。 我無法弄清楚可用示例中使用的文檔和 javascript 回調。 請幫忙!

我希望我能幫助回答你的問題。 使用 pdf2json 可用於解析 pdf 並提取文本。 需要采取幾個步驟才能使其正常工作。 我已經改編了https://github.com/modesty/pdf2json 中的示例。

設置是在node app中安裝pdf2json,還有下划線。 示例頁面沒有解釋定義您自己的回調函數的必要性。 它還使用self而不是this來注冊它們。 因此,通過適當的更改,從 pdf 中提取所有文本的代碼將是這樣的:

// Get the dependencies that have already been installed
// to ./node_modules with `npm install <dep>`in the root director
// of your app 

var _ = require('underscore'),
    PDFParser = require('pdf2json');

var pdfParser = new PDFParser();

// Create a function to handle the pdf once it has been parsed.
// In this case we cycle through all the pages and extraxt
// All the text blocks and print them to console.
// If you do `console.log(JSON.stringify(pdf))` you will 
// see how the parsed pdf is composed. Drill down into it
// to find the data you are looking for.
var _onPDFBinDataReady = function (pdf) {
  console.log('Loaded pdf:\n');
  for (var i in pdf.data.Pages) {
    var page = pdf.data.Pages[i];
    for (var j in page.Texts) { 
      var text = page.Texts[j];
      console.log(text.R[0].T);
    }
  }
};

// Create an error handling function
var _onPDFBinDataError = function (error) {
  console.log(error);
};

// Use underscore to bind the data ready function to the pdfParser
// so that when the data ready event is emitted your function will
// be called. As opposed to the example, I have used `this` instead
// of `self` since self had no meaning in this context
pdfParser.on('pdfParser_dataReady', _.bind(_onPDFBinDataReady, this));

// Register error handling function
pdfParser.on('pdfParser_dataError', _.bind(_onPDFBinDataError, this));

// Construct the file path of the pdf
var pdfFilePath = 'test3.pdf';

// Load the pdf. When it is loaded your data ready function will be called.
pdfParser.loadPDF(pdfFilePath);

我正在運行服務器端控制器中的代碼。

module.exports = (function() {
return {
    add: function(req, res) {
        var tmp_path = req.files.pdf.path;
        var target_path = './uploads/' + req.files.pdf.name;
        fs.rename(tmp_path, target_path, function(err) {
            if (err) throw err;
            // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
            fs.unlink(tmp_path, function() {
                if (err) throw err;
            //edit here pdf parser

            res.redirect('#/');

            });
        })
    },
    show: function(req, res) {

    var pdfParser = new PDFParser();

    var _onPDFBinDataReady = function (pdf) {
      console.log('Loaded pdf:\n');

      for (var i in pdf.data.Pages) {

        var page = pdf.data.Pages[i];
        // console.log(page.Texts);
        for (var j in page.Texts) { 
          var text = page.Texts[j];
          // console.log(text.R[0].T);

        }
      }
      console.log(JSON.stringify(pdf));
    };
    // Create an error handling function
    var _onPDFBinDataError = function (error) {
      console.log(error);
    };
    pdfParser.on('pdfParser_dataReady', _.bind(_onPDFBinDataReady, this));
    // Register error handling function
    pdfParser.on('pdfParser_dataError', _.bind(_onPDFBinDataError, this));
    // Construct the file path of the pdf
    var pdfFilePath = './uploads/Invoice_template.pdf';
    // Load the pdf. When it is loaded your data ready function will be called.
    pdfParser.loadPDF(pdfFilePath);

},

//end controller

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM