Count pages in a PDF
Loads a PDF in memory and returns the page count.
typescript
import { PDFDocument } from "pdf-lib";
export async function countPdfPages(file: File): Promise<number> {
const bytes = await file.arrayBuffer();
const pdf = await PDFDocument.load(bytes, { ignoreEncryption: true });
return pdf.getPageCount();
}Dependencies
pdf-libUsage notes
- Takes a browser File (e.g. from a file input).
- It's async: use it with await or .then().
Limitations
- An encrypted or damaged PDF may not be readable.
- It only counts pages; it doesn't extract text or generate thumbnails.