Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: replace react-pdf page count with libpdf

Marc Seitz9bf07f8fda51
diff --git a/lib/utils/get-page-number-count.ts b/lib/utils/get-page-number-count.ts
index 790b9e34..bad24af1 100644
--- a/lib/utils/get-page-number-count.ts
+++ b/lib/utils/get-page-number-count.ts
@@ -1,40 +1,18 @@
-import { pdfjs } from "react-pdf";
+import { PDF, SecurityError } from "@libpdf/core";
import * as XLSX from "xlsx";
-// Default to CDN worker URL
-const cdnWorkerUrl = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
-pdfjs.GlobalWorkerOptions.workerSrc = cdnWorkerUrl;
-
export const getPagesCount = async (arrayBuffer: ArrayBuffer) => {
try {
- // Only in browser context
- if (typeof window !== "undefined") {
- try {
- // First attempt with the current worker configuration
- const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
- return pdf.numPages;
- } catch (workerError) {
- console.warn("PDF worker error, trying fallback:", workerError);
-
- // Fall back to local worker
- pdfjs.GlobalWorkerOptions.workerSrc = `/pdf.worker.min.js`;
-
- try {
- // Try again with local worker
- const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
- return pdf.numPages;
- } catch (fallbackError) {
- console.warn("Both CDN and local worker failed:", fallbackError);
- return 1; // Default to 1 page if both attempts fail
- }
- }
+ const bytes = new Uint8Array(arrayBuffer);
+ const pdf = await PDF.load(bytes);
+ console.log("PDF page count:", pdf.getPageCount());
+ return pdf.getPageCount();
+ } catch (error) {
+ if (error instanceof SecurityError) {
+ console.warn("PDF is password-protected, cannot determine page count");
} else {
- // Server-side rendering case
- const pdf = await pdfjs.getDocument(arrayBuffer).promise;
- return pdf.numPages;
+ console.error("Error getting PDF page count:", error);
}
- } catch (error) {
- console.error("Error getting PDF page count:", error);
return 1; // Assuming at least one page if we can't determine
}
};
diff --git a/next.config.mjs b/next.config.mjs
index d5944c34..25d8613a 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -153,6 +153,16 @@ const nextConfig = {
},
missingSuspenseWithCSRBailout: false,
},
+ webpack: (config) => {
+ // Stub out @google-cloud/kms - it's an optional dependency of @libpdf/core
+ // that we don't use (only needed for KMS-based PDF encryption)
+ config.resolve.alias = {
+ ...config.resolve.alias,
+ "@google-cloud/kms": false,
+ "@google-cloud/secret-manager": false,
+ };
+ return config;
+ },
};
function prepareRemotePatterns() {
diff --git a/package-lock.json b/package-lock.json
index 2e82201c..de69e800 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -25,6 +25,7 @@
"@github/webauthn-json": "^2.1.1",
"@hookform/resolvers": "^5.2.2",
"@jitsu/js": "^1.10.4",
+ "@libpdf/core": "^0.2.5",
"@next-auth/prisma-adapter": "^1.0.7",
"@next/third-parties": "^16.1.6",
"@pdf-lib/fontkit": "^1.1.1",
@@ -3098,6 +3099,40 @@
"resolved": "https://registry.npmjs.org/@jsonhero/path/-/path-1.0.21.tgz",
"integrity": "sha512-gVUDj/92acpVoJwsVJ/RuWOaHyG4oFzn898WNGQItLCTQ+hOaVlEaImhwE1WqOTf+l3dGOUkbSiVKlb3q1hd1Q=="
},
+ "node_modules/@libpdf/core": {
+ "version": "0.2.5",
+ "resolved": "https://registry.npmjs.org/@libpdf/core/-/core-0.2.5.tgz",
+ "integrity": "sha512-+oTpNRkEdL1kVmeJr6qz2wf0yqJx5FmUVN2u0kDuX81wvxyzYOlMjmFD8qbbJqyYiNZp0J7IAcW6VsZr+MW1Uw==",
+ "license": "MIT",
+ "dependencies": {
+ "@noble/ciphers": "^2.1.1",
+ "@noble/hashes": "^2.0.1",
+ "@scure/base": "^2.0.0",
+ "pako": "^2.1.0",
+ "pkijs": "^3.3.3"
+ },
+ "engines": {
+ "node": ">=20"
+ },
+ "peerDependencies": {
+ "@google-cloud/kms": "^5.0.0",
+ "@google-cloud/secret-manager": "^6.0.0"
+ },
+ "peerDependenciesMeta": {
+ "@google-cloud/kms": {
+ "optional": true
+ },
+ "@google-cloud/secret-manager": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/@libpdf/core/node_modules/pako": {
+ "version": "2.1.0",
+ "resolved": "https://registry.npmjs.org/pako/-/pako-2.1.0.tgz",
+ "integrity": "sha512-w+eufiZ1WuJYgPXbV/PO3NCMEc3xqylkKHzp8bxp1uW4qaSNQUkwmLLEc3kKsfz8lpV1F8Ht3U1Cm+9Srog2ug==",
+ "license": "(MIT AND Zlib)"
+ },
"node_modules/@mapbox/node-pre-gyp": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/@mapbox/node-pre-gyp/-/node-pre-gyp-1.0.11.tgz",
@@ -3397,6 +3432,30 @@
"react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0"
}
},
+ "node_modules/@noble/ciphers": {
+ "version": "2.1.1",
+ "resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-2.1.1.tgz",
+ "integrity": "sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 20.19.0"
+ },
+ "funding": {
+ "url": "https://paulmillr.com/funding/"
+ }
+ },
+ "node_modules/@noble/hashes": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.0.1.tgz",
+ "integrity": "sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 20.19.0"
+ },
+ "funding": {
+ "url": "https://paulmillr.com/funding/"
+ }
+ },
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@@ -6311,6 +6370,15 @@
"resolved": "https://registry.npmjs.org/@rushstack/eslint-patch/-/eslint-patch-1.10.4.tgz",
"integrity": "sha512-WJgX9nzTqknM393q1QJDJmoW28kUfEnybeTfVNcNAPnIx210RXm2DiXiHzfNPJNIUUb1tJnz/l4QGtJ30PgWmA=="
},
+ "node_modules/@scure/base": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/@scure/base/-/base-2.0.0.tgz",
+ "integrity": "sha512-3E1kpuZginKkek01ovG8krQ0Z44E3DHPjc5S2rjJw9lZn3KSQOs8S7wqikF/AH7iRanHypj85uGyxk0XAyC37w==",
+ "license": "MIT",
+ "funding": {
+ "url": "https://paulmillr.com/funding/"
+ }
+ },
"node_modules/@selderee/plugin-htmlparser2": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/@selderee/plugin-htmlparser2/-/plugin-htmlparser2-0.11.0.tgz",
@@ -9238,6 +9306,20 @@
"url": "https://github.com/sponsors/ljharb"
}
},
+ "node_modules/asn1js": {
+ "version": "3.0.7",
+ "resolved": "https://registry.npmjs.org/asn1js/-/asn1js-3.0.7.tgz",
+ "integrity": "sha512-uLvq6KJu04qoQM6gvBfKFjlh6Gl0vOKQuR5cJMDHQkmwfMOQeN3F3SHCv9SNYSL+CRoHvOGFfllDlVz03GQjvQ==",
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "pvtsutils": "^1.3.6",
+ "pvutils": "^1.1.3",
+ "tslib": "^2.8.1"
+ },
+ "engines": {
+ "node": ">=12.0.0"
+ }
+ },
"node_modules/ast-types-flow": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/ast-types-flow/-/ast-types-flow-0.0.8.tgz",
@@ -9602,6 +9684,15 @@
"node": ">=10.16.0"
}
},
+ "node_modules/bytestreamjs": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/bytestreamjs/-/bytestreamjs-2.0.1.tgz",
+ "integrity": "sha512-U1Z/ob71V/bXfVABvNr/Kumf5VyeQRBEm6Txb0PQ6S7V5GpBM3w4Cbqz/xPDicR5tN0uvDifng8C+5qECeGwyQ==",
+ "license": "BSD-3-Clause",
+ "engines": {
+ "node": ">=6.0.0"
+ }
+ },
"node_modules/call-bind": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.8.tgz",
@@ -17037,6 +17128,35 @@
"pathe": "^2.0.1"
}
},
+ "node_modules/pkijs": {
+ "version": "3.3.3",
+ "resolved": "https://registry.npmjs.org/pkijs/-/pkijs-3.3.3.tgz",
+ "integrity": "sha512-+KD8hJtqQMYoTuL1bbGOqxb4z+nZkTAwVdNtWwe8Tc2xNbEmdJYIYoc6Qt0uF55e6YW6KuTHw1DjQ18gMhzepw==",
+ "license": "BSD-3-Clause",
+ "dependencies": {
+ "@noble/hashes": "1.4.0",
+ "asn1js": "^3.0.6",
+ "bytestreamjs": "^2.0.1",
+ "pvtsutils": "^1.3.6",
+ "pvutils": "^1.1.3",
+ "tslib": "^2.8.1"
+ },
+ "engines": {
+ "node": ">=16.0.0"
+ }
+ },
+ "node_modules/pkijs/node_modules/@noble/hashes": {
+ "version": "1.4.0",
+ "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz",
+ "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 16"
+ },
+ "funding": {
+ "url": "https://paulmillr.com/funding/"
+ }
+ },
"node_modules/possible-typed-array-names": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.0.0.tgz",
@@ -17944,6 +18064,24 @@
"node": ">=6"
}
},
+ "node_modules/pvtsutils": {
+ "version": "1.3.6",
+ "resolved": "https://registry.npmjs.org/pvtsutils/-/pvtsutils-1.3.6.tgz",
+ "integrity": "sha512-PLgQXQ6H2FWCaeRak8vvk1GW462lMxB5s3Jm673N82zI4vqtVUPuZdffdZbPDFRoU8kAhItWFtPCWiPpp4/EDg==",
+ "license": "MIT",
+ "dependencies": {
+ "tslib": "^2.8.1"
+ }
+ },
+ "node_modules/pvutils": {
+ "version": "1.1.5",
+ "resolved": "https://registry.npmjs.org/pvutils/-/pvutils-1.1.5.tgz",
+ "integrity": "sha512-KTqnxsgGiQ6ZAzZCVlJH5eOjSnvlyEgx1m8bkRJfOhmGRqfo5KLvmAlACQkrjEtOQ4B7wF9TdSLIs9O90MX9xA==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=16.0.0"
+ }
+ },
"node_modules/qs": {
"version": "6.14.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.14.1.tgz",
diff --git a/package.json b/package.json
index 9b93d001..a19ad07c 100644
--- a/package.json
+++ b/package.json
@@ -36,6 +36,7 @@
"@github/webauthn-json": "^2.1.1",
"@hookform/resolvers": "^5.2.2",
"@jitsu/js": "^1.10.4",
+ "@libpdf/core": "^0.2.5",
"@next-auth/prisma-adapter": "^1.0.7",
"@next/third-parties": "^16.1.6",
"@pdf-lib/fontkit": "^1.1.1",