【发布时间】:2020-07-19 14:35:12
【问题描述】:
我无法使用 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)
Nodejs
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'
}
【问题讨论】:
-
好的,您可以使用命令
node filename.js直接运行应用程序并显示控制台输出吗? -
我添加了我的问题节点 app.js。请检查一下。
-
看起来加载函数需要回调作为第二个参数。
-
尝试将其更改为
pdfParser.loadPDF("/Desktop/nodejs-pdf-parse/pdf/Paycheck-Protection.pdf", (e) => {console.log(e)});并在控制台上显示输出。 -
TypeError [ERR_INVALID_CALLBACK]:回调必须是函数。在 PdfParser.
(/home/mutlueren/Desktop/nodejs-pdf-parse/app.js: 9:8) 在 PdfParser.emit (events.js:315:20) 在 PdfParser._onPDFJSParseDataReady (/home/mutlueren/Desktop/nodejs-pdf-
标签: javascript node.js json parsing