Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk remove pages fails after 8 pages #440

Closed
jackwshepherd opened this issue May 9, 2020 · 8 comments
Closed

Bulk remove pages fails after 8 pages #440

jackwshepherd opened this issue May 9, 2020 · 8 comments

Comments

@jackwshepherd
Copy link

jackwshepherd commented May 9, 2020

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?

@Hopding
Copy link
Owner

Hopding commented May 9, 2020

Hello @jackwshepherd!

What version of pdf-lib are you using?

@jackwshepherd
Copy link
Author

jackwshepherd commented May 9, 2020

1.4.1. I just upgraded to 1.5.0 and still get that error

@Hopding
Copy link
Owner

Hopding commented May 9, 2020

Can you please share a document I can use to reproduce the issue?

@jackwshepherd
Copy link
Author

Just emailed it to you - thanks a lot. Hopefully we can understand why, and post the response here. Just didn't want the doc to be public.

@jackwshepherd
Copy link
Author

It seems to work fine with PDFs that are text - seems to arise only with PDFs that are comprised of images. Very odd.

@Hopding
Copy link
Owner

Hopding commented May 9, 2020

@jackwshepherd I found and fixed the bug that was causing this issue in #441.

@Hopding
Copy link
Owner

Hopding commented May 9, 2020

Version 1.5.1-rc2 is now published. It contains the fix for this issue.

You can install this new version with npm:

npm install pdf-lib@1.5.1-rc2

It's also available on unpkg:

As well as jsDelivr:

@Hopding
Copy link
Owner

Hopding commented May 16, 2020

Version 1.6.0 is now published. It contains the fix for this issue. The full release notes are available here.

You can install this new version with npm:

npm install pdf-lib@1.6.0

It's also available on unpkg:

As well as jsDelivr:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants