-
-
Notifications
You must be signed in to change notification settings - Fork 832
Closed
Description
In my app, there is a functionality that allows people to "split" a long PDF into two PDFs.
The way it currently works is as follows:
- User scrolls through the PDF, and selects the split point. Everything from that point and before will be extracted into a new PDF
- Load the PDF, and create an array of every page that needs removing from the source. (e.g. if we make the split point Page 5, the array will be [0 , 1, 2, 3, 4].
This is the code:
// Initiate PDFDocument object
let pdf = await PDFDocument.create();
// Now load into a PDFDocument object
const file = await readFileAsync(filename);
const source = await PDFDocument.load(file);
// Make an array of all pages to extract
let newPages = [];
for (i = 0; i < splitPoint; i++) {
newPages.push(i);
}
// Copy those pages to a new PDF
const copiedPages = await pdf.copyPages(source, newPages);
copiedPages.forEach(page => pdf.addPage(page));
const newEntry = await pdf.save();
// Now remove pages from the source document and save back
newPages.forEach(page => {
source.removePage(0);
});
const newSource = await source.save();
Whenever the newPages array is greater than 8, I always get this error - regardless of the PDF. By this point I have already error checked to see that the "split point" is actually within the document, i.e. that there are in fact at least 9 pages in the document. So this error is not being produced by trying to remove a page when the document doesn't have that page contained within it.
Error: Index out of bounds: 0/-1 (b)
[0] at PDFPageTree.removeLeafNode (/Users/me/workspace/app/server/node_modules/pdf-lib/cjs/core/structures/PDFPageTree.js:105:19)
[0] at PDFPageTree.removeLeafNode (/Users/me/workspace/app/server/node_modules/pdf-lib/cjs/core/structures/PDFPageTree.js:109:20)
[0] at PDFCatalog.removeLeafNode (/Users/me/workspace/app/server/node_modules/pdf-lib/cjs/core/structures/PDFCatalog.js:27:22)
[0] at PDFDocument.removePage (/Users/me/workspace/app/server/node_modules/pdf-lib/cjs/api/PDFDocument.js:338:22)
[0] at /Users/me/workspace/app/server/routes/pdfRoutes.js:73:16
[0] at Array.forEach (<anonymous>)
[0] at /Users/me/workspace/app/server/routes/pdfRoutes.js:72:16
Any ideas? Is there a better way to do this?
Metadata
Metadata
Assignees
Labels
No labels