节点js应用程序中的pdf2json解析错误

我无法使用 pdf2json 将 pdf 转换为 json 格式。我想通过nodejs使用pdf2json库将pdf文件转换为json格式。但是,我为json指定的目录中没有json文件,我放一个空的json文件时它并没有过期。当我在调试模式下运行我的代码时,它会输出以下日志。我该怎么做才能保存为json?


我遵循这些步骤; https://github.com/modesty/pdf2json


控制台日志消息


/Desktop/nodejs-pdf-parse/app.js

Debugger listening on ws://127.0.0.1:45843/bb0e57e8-b28d-4652-81f8-2cba00f4372b

For help, see: https://nodejs.org/en/docs/inspector

Debugger attached.

Waiting for the debugger to disconnect...


Process finished with exit code 130 (interrupted by signal 2: SIGINT)

节点


let fs = require('fs'), PDFParser = require("pdf2json");


let pdfParser = new PDFParser(this, 1);


try {

    pdfParser.loadPDF("/Desktop/nodejs-pdf-parse/pdf/Paycheck-Protection.pdf");

}catch (e) {

    console.log(e)

}


pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );

pdfParser.on("pdfParser_dataReady", pdfData => {

    fs.writeFile("/Desktop/nodejs-pdf-parse/parsed-json/parsed.json", JSON.stringify(pdfData));

});

运行:节点 app.js


fs.js:148

  throw new ERR_INVALID_CALLBACK(cb);

  ^


TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined

    at maybeCallback (fs.js:148:9)

    at Object.writeFile (fs.js:1298:14)

    at PdfParser.<anonymous> (/home/mutlueren/Desktop/nodejs-pdf-parse/app.js:7:8)

    at PdfParser.emit (events.js:315:20)

    at PdfParser._onPDFJSParseDataReady (/home/mutlueren/Desktop/nodejs-pdf-parse/node_modules/pdf2json/pdfparser.js:25:9)

    at cls.emit (events.js:315:20)

    at /home/mutlueren/Desktop/nodejs-pdf-parse/node_modules/pdf2json/lib/pdf.js:250:38

    at processTicksAndRejections (internal/process/task_queues.js:79:11) {

  code: 'ERR_INVALID_CALLBACK'

}


UYOU
浏览 374回答 1
1回答

忽然笑

const pdfParser = new PDFParser();&nbsp; pdfParser.on("pdfParser_dataError", (errData) =>&nbsp; &nbsp; console.error(errData.parserError)&nbsp; );&nbsp; pdfParser.on("pdfParser_dataReady", (pdfData) => {&nbsp; &nbsp; fs.writeFile(&nbsp; &nbsp; &nbsp; "/Desktop/nodejs-pdf-parse/parsed-json/parsed.json",&nbsp; &nbsp; &nbsp; JSON.stringify(pdfData),&nbsp; &nbsp; &nbsp; function (err, result) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(err);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );&nbsp; });&nbsp; pdfParser.loadPDF("/Desktop/nodejs-pdf-parse/pdf/Paycheck-Protection.pdf");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript