Skip to content
Advertisement

COSStream has been closed and cannot be read

I have next code in my project and time to time it falls with COSStream has been closed and cannot be read. Perhaps its enclosing PDDocument has been closed? It happens in different time and with different workload, so I want to fix it. Thanks in advance.

JavaScript

and here part that load resourse:

JavaScript

Advertisement

Answer

You use streams from template documents (templatePDDocument, fontTemplatePdf) created anew and also given free for garbage collection in each loop iteration. Thus, some of these template documents may have been finalized by garbage collection before you call outPDDocument.save, causing the errors you observe.

If you keep this base architecture, you can prevent that those template documents are finalized prematurely by adding them all to some collection and clearing that collection only after calling outPDDocument.save.

Alternatively you can switch to cloning the template pages and using the clones instead of using the original template pages.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement