From 3b2b8c7323660b69f80b9e37bb3694a08238c32a Mon Sep 17 00:00:00 2001 From: Fabien Lallemand Date: Mon, 3 Aug 2026 16:24:19 +0200 Subject: [PATCH] feat: :sparkles: expose getPDFPageCount to return the number of pages in a pdf --- CHANGELOG.md | 6 ++++++ docs/docs/Utils/get-pdf-page-count.mdx | 15 +++++++++++++++ src/index.ts | 2 ++ src/utils/getPDFPageCount.spec.tsx | 18 ++++++++++++++++++ src/utils/getPDFPageCount.ts | 16 ++++++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 docs/docs/Utils/get-pdf-page-count.mdx create mode 100644 src/utils/getPDFPageCount.spec.tsx create mode 100644 src/utils/getPDFPageCount.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d7de2b..b758dc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## v1.7.0 + +### Changes + +- expose `getPDFPageCount` to return the number of pages in a PDF without rendering + ## v1.6.9 (26/01/2026) ### Fixes diff --git a/docs/docs/Utils/get-pdf-page-count.mdx b/docs/docs/Utils/get-pdf-page-count.mdx new file mode 100644 index 0000000..4d2c976 --- /dev/null +++ b/docs/docs/Utils/get-pdf-page-count.mdx @@ -0,0 +1,15 @@ +--- +sidebar_position: 2 +--- + +# getPDFPageCount + +- Type : `(file: string) => Promise` + +> The parameter should be a valid Object URL of type string. If you have an object of type [File](https://developer.mozilla.org/fr/docs/Web/API/File) ( typically generated by file upload libraries) in hand you must use [URL.createObjectURL()](https://developer.mozilla.org/fr/docs/Web/API/URL/createObjectURL) to generate a valid URL object. + +```javascript +getPDFPageCount(file).then((pageCount) => { + // Do something with the page count +}) +``` diff --git a/src/index.ts b/src/index.ts index de5968c..a072489 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,7 @@ import { toBase64, } from './utils/functions' import getImagesFromPDF from './utils/getImagesFromPDF' +import getPDFPageCount from './utils/getPDFPageCount' import { dataURItoBlob } from './utils/image' export type { @@ -36,6 +37,7 @@ export { AnnotationLens, AnnotationViewer, getImagesFromPDF, + getPDFPageCount, drawShape, drawLayer, setShapeConfig, diff --git a/src/utils/getPDFPageCount.spec.tsx b/src/utils/getPDFPageCount.spec.tsx new file mode 100644 index 0000000..04f1223 --- /dev/null +++ b/src/utils/getPDFPageCount.spec.tsx @@ -0,0 +1,18 @@ +import brokenPDF from 'cypress/assets/broken-pdf.pdf' +import multiPage from 'cypress/assets/multi-page.pdf' + +import getPDFPageCount from './getPDFPageCount' + +describe('getPDFPageCount', () => { + it('should return the number of PDF pages', () => { + getPDFPageCount(multiPage).then((pageCount) => { + expect(pageCount).to.equal(5) + }) + }) + + it('should catch error when the PDF is broken', () => { + getPDFPageCount(brokenPDF).catch((error) => { + expect(error.name).to.equal('InvalidPDFException') + }) + }) +}) diff --git a/src/utils/getPDFPageCount.ts b/src/utils/getPDFPageCount.ts new file mode 100644 index 0000000..992edb6 --- /dev/null +++ b/src/utils/getPDFPageCount.ts @@ -0,0 +1,16 @@ +import { + getDocument, + GlobalWorkerOptions, + PDFDocumentProxy, + version, +} from 'pdfjs-dist' + +GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${version}/pdf.worker.js` + +export default function getPDFPageCount(file: string) { + return new Promise((resolve, reject) => { + getDocument(file) + .promise.then((document: PDFDocumentProxy) => resolve(document.numPages)) + .catch((error) => reject(error)) + }) +}