Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions docs/docs/Utils/get-pdf-page-count.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
sidebar_position: 2
---

# getPDFPageCount

- Type : `(file: string) => Promise<number>`

> 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
})
```
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,6 +37,7 @@ export {
AnnotationLens,
AnnotationViewer,
getImagesFromPDF,
getPDFPageCount,
drawShape,
drawLayer,
setShapeConfig,
Expand Down
18 changes: 18 additions & 0 deletions src/utils/getPDFPageCount.spec.tsx
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
16 changes: 16 additions & 0 deletions src/utils/getPDFPageCount.ts
Original file line number Diff line number Diff line change
@@ -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<number>((resolve, reject) => {
getDocument(file)
.promise.then((document: PDFDocumentProxy) => resolve(document.numPages))
.catch((error) => reject(error))
})
}
Loading