parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
29.37k stars 4.68k forks source link

Memory Management for Large PDF Documents in jsPDF #3756

Open JeelGajera opened 3 months ago

JeelGajera commented 3 months ago

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:

Environment:

indraraj26 commented 2 months ago

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 commented 2 months ago

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