Hummus-recipe NPM: TypeError: Unable to start parsing PDF file

1.1k Views Asked by At
const HummusRecipe = require('hummus-recipe');
const pdfDoc = new HummusRecipe(inputFilePath, outputFilePath);

Above code will raise error TypeError: Unable to start parsing PDF file in hummus-recipe library if the input PDF is in invalid standard PDF Format.

1

There are 1 best solutions below

0
On

Finally, I've created a solution to deal with this error in hummus-recipe It's caused due to invalid PDF Format that's not acceptable by hummus-recipe.

exports.PDFConversionToHummusFormat = async (inputPath, outputPath) => {
const { PDFDocument } = require('pdf-lib');
const HummusRecipe = require('hummus-recipe');
const fileBytes = fs.readFileSync(inputPath);
const convertedPdf = await PDFDocument.load(fileBytes);
const pdfBytes = await convertedPdf.save();
fs.writeFileSync(inputPath, pdfBytes);
const pdfDoc = new HummusRecipe(inputPath, outputPath);
return pdfDoc;
};


const HummusRecipe = require('hummus-recipe');
let pdfDoc;
try {
        pdfDoc = new HummusRecipe(inputPath, outputPath);
} catch (error) {
  if (error.message === 'TypeError: Unable to start parsing PDF file') {
        pdfDoc = await PDFConversionToHummusFormat(inputPath, outputPath);
  } else {
      console.error('Error in Hummus Recipe Library ->', error);
  }
}

Note: You can check if your PDF is valid or not at https://www.pdfen.com/pdf-a-validator OR You can convert your PDF to a valid version at https://docupub.com/pdfconvert/