Node.js Tutorial to Convert PDF Documents to JSON File Using pdf2json Library in Javascript Full Project For Beginners

 

 

npm init -y

 

 

npm i pdf2json

 

 

After installing this library make an index.js file and copy paste the following code

 

 

index.js

 

 

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

    let pdfParser = new PDFParser();

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("./pdf2json/test/F1040EZ.json", JSON.stringify(pdfData));
    });

    pdfParser.loadPDF("./pdf2json/test/pdf/fd/form/F1040EZ.pdf");

 

 

Or, call directly with buffer:

    fs.readFile(pdfFilePath, (err, pdfBuffer) => {
      if (!err) {
        pdfParser.parseBuffer(pdfBuffer);
      }
    })
  • Parse a PDF then write a .txt file (which only contains textual content of the PDF)
    let fs = require('fs'),
        PDFParser = require("pdf2json");

    let pdfParser = new PDFParser(this,1);

    pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
    pdfParser.on("pdfParser_dataReady", pdfData => {
        fs.writeFile("./pdf2json/test/F1040EZ.content.txt", pdfParser.getRawTextContent());
    });

    pdfParser.loadPDF("./pdf2json/test/pdf/fd/form/F1040EZ.pdf");

Leave a Reply