You can also use pdf-lib .
You will need to read the file from the input field and then use pdf-lib to get the number of pages. The code will look like this:
import { PDFDocument } from 'pdf-lib'; ... const readFile = (file) => { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); reader.readAsArrayBuffer(file); }); } const async getNumPages = (file) => { const arrayBuffer = await readFile(file); const pdf = await PDFDocument.load(arrayBuffer); return pdf.getPages(); }
And then just get the number of pages of the attached file with:
const numPages = await getNumPages(input.files[0]);
being an input variable that stores a reference to the DOM element of the input file.
José Antonio Postigo
source share