【问题标题】:local PDF file scraping in node.jsnode.js 中的本地 PDF 文件抓取
【发布时间】:2015-07-28 23:17:04
【问题描述】:

我已经使用 fs 通过 MEAN 堆栈 Web 应用程序上传了一个 pdf。我想从 pdf 中提取某些字段并将它们显示在网络应用程序上。我看过几个 npm 包,比如 pdf.js、pdf2json。我无法弄清楚可用示例中使用的文档和 javascript 回调。请帮忙!

【问题讨论】:

    标签: javascript node.js pdf


    【解决方案1】:

    我希望我能帮助回答你的问题。使用 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);
    

    【讨论】:

    • 感谢您的详细回复。我一直在尝试挖掘数据,但它看起来就像我在 dataready 函数加载后的 console.log(JSON.stringify(pdf)) 时一样。它不会显示除 %n 之外的任何数据。它似乎没有从 pdf 中获取数据。 console.log(text.R[0].T) 似乎正在记录 pdf,但打印出非常分散的 PDF 文本片段。
    • 请发布您为 onDataReady 注册的功能。我用一些基本的 PDF 尝试过这个,但我现在不知道它如何处理复杂的 PDF。如果您可以提供您正在解析的相同格式 PDF 的示例,我可以尝试。
    • 我在我的一个服务器端控制器中使用上面列出的完全相同的功能。
    • 对于 pdf,我使用的是发票模板/pdf。它应该是一个普通的 pdf 我也尝试过只有文本的。将用我的代码发布答案。
    • 在分配页面后尝试在 for 循环中执行 JSON.stringify(page)。您可能需要使用不同的键进行探索,因为 PDF 中可能有大量数据。我也试过 JSON.stringify(pdf) 在这里也不起作用,也许对象太大了,但是它适用于单个页面。
    【解决方案2】:

    我正在从我的服务器端控制器运行代码。

    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
    

    }

    【讨论】:

    • 我的客户端控制器自动运行显示功能。上传的 pdf 在我的上传文件夹中,解析函数指向该文件夹。 “Invoice_Template.pdf”。这个应用程序的想法是从上传文件夹中的 pdf 列表中抓取数据,并能够在 Web 应用程序中列出它们。
    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2022-12-17
    • 2016-12-12
    • 2017-08-26
    相关资源
    最近更新 更多