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

Memory Management for Large PDF Documents in jsPDF #3756

Open
JeelGajera opened this issue Aug 2, 2024 · 2 comments
Open

Memory Management for Large PDF Documents in jsPDF #3756

JeelGajera opened this issue Aug 2, 2024 · 2 comments

Comments

@JeelGajera
Copy link

Description:

I am encountering significant memory management issues while generating large PDF documents using jsPDF. My use case involves creating a PDF with around 18,000 pages from a 27MB JSON data file. While this works in a local environment, it becomes problematic in serverless environments such as Google Cloud Functions due to memory constraints.

Actual Behavior:

The PDF generation process consumes over 4GB of heap memory, making it impossible to complete in a serverless environment with limited memory. The resulting PDF document object becomes unmanageably large (around 600MB).

I attempted to generate smaller PDFs and merge them using pdf-lib, but this approach complicates maintaining consistent page numbers and links within the document.

Workaround Request:

In the meantime, I am seeking a workaround to manage multiple jsPDF document objects and combine them into a final result. Specifically:

  • How can I generate multiple smaller jsPDF documents and then combine them into a single final PDF while maintaining consistent page numbers and internal links?
  • Is there a recommended approach to handle this scenario efficiently?

Environment:

  • jsPDF Version: 2.5.1
@indraraj26
Copy link

Think of pagination you know the starting and ending.

Get me 10 data and i will print and once it is done I will mark the data it is printed.

Now give me next 10 data... continue the process

I assume you have something unique in the each json to identify the each object or you can use index to identify it.

@JeelGajera
Copy link
Author

thanks @indraraj26 for the answer

well here is the thing I don't know the starting and ending, for example, in my data, there is one section of question & answers sometimes this data will be printed on 2 pages, and sometimes it takes more than 10 pages ( I had managed the code that creates pages dynamically based on the data fits in)

There is no question regarding jsPDF capability, but for the larger data if I use only one object of jsPDF this causes me so much heap, all I'm looking for a solution is how I can manage this process into chunks of files and store them temporary combine them at the end while maintaining my page links (i.e textWithLink() ) so individual jsPDF object don't get so much bigger

or there might be some other approach to handle this kind of scenario

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