Skip to content
Advertisement

pdf2json parse error in node js application

I am having trouble converting pdf to json format using pdf2json. I want to convert a pdf file into json format using the pdf2json library via nodejs. However, there was no json file in the directory I specified for json, and it did not expire when I put an empty json file. When I run my code in debug mode, it outputs the following log. What do I have to do to save as a json?

I follow these steps; https://github.com/modesty/pdf2json

Console log message

/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));
});

run: node 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'
}

Advertisement

Answer

const pdfParser = new PDFParser();
  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),
      function (err, result) {
        console.log(err);
      }
    );
  });
  pdfParser.loadPDF("/Desktop/nodejs-pdf-parse/pdf/Paycheck-Protection.pdf");
Advertisement