Bulk remove pages fails after 8 pages
See original GitHub issueIn 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?
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How to delete multiple pages from a Pages…
Yes. Pages 6 Help says: "In a page layout document: Click the View menu button in the toolbar, choose Show Page Thumbnails, select...
Read more >Tried to delete a page from Organize Pages and Error ...
1. Right click on the left hand pane. · 2. Select Tags. · 3. Select the first item in the list labelled 'Tags'....
Read more >Shortcut to delete multiple page (range of pages) in Ms Word ...
The following steps to delete multiple pages (range of page ) in Ms Word Let's say that you need to delete pages from...
Read more >Error while Bulk delete of Post - WordPress.org
I have currently 18000 post on the website under different categories and i want to delete them but the Plugin of WP Bulk...
Read more >Ability to delete or multiple delete pages from All pages ...
Many times when we browse All pages, there are many accidental pages created. It would be helpful if we can delete it from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Version
1.5.1-rc2is now published. It contains the fix for this issue.You can install this new version with npm:
It’s also available on unpkg:
As well as jsDelivr:
Version
1.6.0is now published. It contains the fix for this issue. The full release notes are available here.You can install this new version with npm:
It’s also available on unpkg:
As well as jsDelivr: