Fast, dependency-free page count extractor for PDF files in Node.js.
npm i pdf-pages-countCommonJS:
const { countPdfPages, countPdfPagesSync } = require("pdf-pages-count");
(async () => {
const pages = await countPdfPages("/path/to/file.pdf");
console.log(pages);
})();
const fs = require("fs");
const buf = fs.readFileSync("/path/to/file.pdf");
const pages2 = countPdfPagesSync(buf);
console.log(pages2);ESM / TypeScript:
import { countPdfPages, countPdfPagesSync } from "pdf-pages-count";
const pages = await countPdfPages("/path/to/file.pdf");
const pages2 = countPdfPagesSync(new Uint8Array(/* ... */));- Tries classic xref parsing to read
/Root -> /Pages -> /Count. - Falls back to scanning for
/Type /Pagesand nearby/Count N. - Additionally scans deflated streams (e.g., object streams) for those markers.
- As a last resort, counts occurrences of
/Type /Pageacross plain and deflated content.
- Encrypted PDFs are not supported.
- Some exotic PDFs may still evade detection. Open an issue with a sample if that happens.
MIT