How to verify in an automated way that an image is inside a locally downloaded PDF?

Viewed 58

In Cypress I have a test case where I download a PDF and I must verify that inside it is a specific image (along with much more information, the image is only 1/8 of page and the PDF has between 5 and 10 pages, the rest of the information does not interest me, if the image is inside the PDF it should be enough to place the test case as PASS).

I can search for text inside the PDF, using a NPM package called pdf-parse with which I convert the PDF to text (if it helps anyone I put it at the end of the comment just in case) and then I simply search for the string I need, but I can't find a way to search for an image inside the PDF neither with this package nor with any other. Will anyone know a way to do this? Thank you very much.

const pdf = require('pdf-parse');

const parsePDF = async (pathPDF) => {
 const pdfPathname = path.join(repoRoot, pdfName)
 let dataBuffer = fs.readFileSync(pathPDF);
 return await pdf(dataBuffer) 
}

module.exports = (on, config) => {  
 on('task', {
  getPDFText(pathPDF) {
  return parsePDF(pathPDF);
  }
 });
}
1 Answers

Not exactly what I was looking for but I managed to do what I needed by comparing the original PDF with the real PDF with compare-pdf.

Related