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();
}Dependencias
pdf-libNotas de uso
- Takes a browser File (e.g. from a file input).
- It's async: use it with await or .then().
Limitaciones
- An encrypted or damaged PDF may not be readable.
- It only counts pages; it doesn't extract text or generate thumbnails.