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

[Feature Request]: Clone pages #169

Closed
samezyane opened this issue Aug 23, 2019 · 3 comments
Closed

[Feature Request]: Clone pages #169

samezyane opened this issue Aug 23, 2019 · 3 comments

Comments

@samezyane
Copy link

Hi,

I have an SVG backgound image that repeats in many pages, what I have done so far is the following:

  • create a PDF for the SVG background image 'pdfFromSvg.pdf' (using inkscape)
  • create the final pdf document 'pdfDoc'
    const pdfDoc = await PDFDocument.create()
  • load the 'pdfFromSvg.pdf' in a temporary PDFDocument
    const imgPdfDoc = await PDFDocument.load(base64of('pdfFromSvg.pdf'))
  • loop through my loop and call:
    const [imgPage] = await pdfDoc.copyPages(imgPdfDoc, [0])
    const pdfPage = pdfDoc.insertPage(idx, imgPage)
    do the specific rendering on the newly inserted 'pdfPage'

I think that there is room to enhance the performance if there is a 'copyPage' version of 'copyPages' that copy a single page from a source pdf and that returns multiple instances of the same page,

  • the code will be:
    const pdfDoc = await PDFDocument.create()
    const imgPdfDoc = await PDFDocument.load(base64of('pdfFromSvg.pdf'))
    const imgPages = await pdfDoc.copyPage(imgPdfDoc, 0, N) // copy the first page and return an array of N pages
  • loop through my loop and call:
    const pdfPage = pdfDoc.insertPage(idx, imgPages[idx])
    do the specific rendering on the newly inserted 'pdfPage'

By inserting the same page, my tests show that for a backround image that repeats 70 times:

  • the processing time improves from 3 minutes to 10 seconds
  • and the size of the pdf decreases from 18 MB to 1MB

Regards,

Saeed

@Hopding
Copy link
Owner

Hopding commented Dec 27, 2019

Hello @samezyane!

I think the best way to solve this problem would be to support cloning/copying PDFPage objects. And I certainly agree that this would be a useful feature to provide.

Until this feature lands in pdf-lib, I've created a workaround you can use in #47 (comment). Please take a look at the full example, but here's the gist of it:

/* Copies a PDFPage object */
const copyPage = (originalPage) => { ... };

const pdfDoc = await PDFDocument.create();
const donorDoc = await PDFDocument.load(...);

const [page1] = await pdfDoc.copyPages(donorDoc, [0]);

const page2 = copyPage(page1);
const page3 = copyPage(page1);

pdfDoc.addPage(page1);
pdfDoc.addPage(page2);
pdfDoc.addPage(page3);

// Do page-specific rendering here...

I hope this helps. Please let me know if you have any additional questions!

@Hopding Hopding changed the title Enhancement: Is there a way to clone a page and insert it in the document? [Feature Request]: Clone pages Dec 27, 2019
@Hopding
Copy link
Owner

Hopding commented Sep 24, 2021

Added this to the roadmap for tracking: #998.

@mericaliyigit
Copy link

Library can really benefit from a hardcopy/clone functionality.
I hit a similar bug today while trying to write some text on to the pdf and got it spread over multiple copied pages.

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

No branches or pull requests

3 participants