【问题标题】:How to read the content of a .pdf file using nodejs?如何使用 nodejs 读取 .pdf 文件的内容?
【发布时间】:2017-01-13 13:17:47
【问题描述】:

我有一个自动化 PDF 内容的方案。如何在nodejs中检索PDF文件的内容。

我完全被阻止了。尽管pdf2jsonajsonreader 上的帖子很少,但这些对我不起作用。任何帮助将不胜感激。

var pdfParser = new PDFParser();
fs.readFile(pdfFilePath, function(err, pdfBuffer) {
    pdfParser.parseBuffer(pdfBuffer);
}, function(pdfBuffer){
    pdfParser.parseBuffer(pdfBuffer);
})

错误:参数数组无效,需要 .data 或 .url 在 FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)

【问题讨论】:

  • 嗨。您发现这 2 个库是否存在特定问题?
  • 嗨 Sebas,我在问题本身中添加了代码 sn-p 和错误。请看一下,让我知道我是 nodejs 的新手
  • 我不确定错误来自 pdfParser,而是来自 fs 对象。

标签: node.js pdf


【解决方案1】:

我找到了答案,它运行良好。通过运行以下命令安装 fs 和 pdf2json。 npm install pdf2jsonnpm install fs

var fs = require('fs');
var PDFParser = require('pdf2json');
var path = osHomedir();
var homepath = path.replace(new RegExp('\\' + path.sep, 'g'), '/');
var pdfFilePath = homepath + '/Downloads/' + 'filename.pdf';

if (fs.existsSync(pdfFilePath)) {
  //Read the content of the pdf from the downloaded path
  var pdfParser = new PDFParser(browser, 1);
  pdfParser.on("pdfParser_dataError", function (errData) {
     console.error(errData.parserError)
  });
  pdfParser.on("pdfParser_dataReady", function (pdfData) {
  //console.log('here is the content: '+pdfParser.getRawTextContent());
  browser.assert.ok(pdfParser.getRawTextContent().indexOf(textToVerify) > -1);
  });

  pdfParser.loadPDF(pdfFilePath);
} else {
    console.log('OOPs file not present in the downloaded folder');
    //Throw an error if the file is not found in the path mentioned
    browser.assert.ok(fs.existsSync(pdfFilePath));
}

【讨论】:

  • 你能告诉我这里的浏览器是什么吗?我也在使用相同但我收到此错误:ReferenceError: browser is not defined 谢谢
  • 你需要解释一下browser这个变量是什么。
  • npmjs.com/package/pdf2json 的文档是使用 pdf2json 的一个很好的起点
【解决方案2】:
 const fs = require("fs");
 const PdfReader = require('pdfreader').PdfReader;
  fs.readFile("E://file streaming in node js//demo//read.pdf", (err, pdfBuffer) => {
    // pdfBuffer contains the file content
    new PdfReader().parseBuffer(pdfBuffer, function(err, item){
       if (err)
           callback(err);
        else if (!item)
            callback();
         else if (item.text)
            console.log(item.text);
          });
       });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-17
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多