foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.88k stars 1.15k forks source link

Cache using all available memory in large document with unique strings #932

Open lekoder opened 5 years ago

lekoder commented 5 years ago

Bug Report

Description of the problem

When generating large document (40k+ pages) with custom font and lots of unique strings, the embeded font layout cache takes up all available memory due to being populated by unique "words". This eventually causes application crash due to out of heap memory error.

Your environment

Use case

Bank statements, accounting reports and other similar documents tend to have lot of unique strings (identifiers, balances, URLs, etc) and are simultaneously very large.

Workaround

An usable workaround was found: passing { features:[] } to .text() disables the cache and allows generation of document.

liborm85 commented 5 years ago

Would be great to add option to disable cache for rendering large documents.

blikblum commented 5 years ago

Would be great to add option to disable cache for rendering large documents.

I will do it.

Another option is to use Map since it can get more keys: https://code.i-harness.com/en/q/1f5cefa

It would be helpful to provide an example so we can test the performance with plain object and Map

blikblum commented 5 years ago

Added. I'd like the opinion about the name. Technically is a fontLayoutCache but the layout is a implementation detail that regular users should not be aware