Skip to content
Advertisement

Issues with enclosing PDDoucment

I have implemented a program that will print data into a pdf however I am facing this issue

JavaScript

I know there have been similar issues posted but I could not find the solution to my problem from them. What I am doing is that I initialized two documents one of them being the main document(doc) the other for the rest of the pages and then I add to the main one(activeDocument), the service calls three different functions to add a new page one for the first page one for any pages in between, one for before the last and finally the last page.a Here is my code most of the logic can be ignored its mainly just the things relating to PDDocuments since thats where the issue lies

JavaScript

There is a lot more logic in these functions but I tried adding only what I thought is relevant.

I believe the issue is concerning either the content stream of one of the pdfs or the order in which I close, save etc…. but I couldn’t figure out what the exact issue is so any advice would be appreciated.

Advertisement

Answer

Three errors are in setPreLastPage, setLastPage, and addExtraPage: in each of these methods you load a new PDDocument in the respective local variable activeDocument, take a page from it and add it to the same PDDocument doc, and then drop all references to that PDDocument in activeDocument when leaving the respective method.

Dropping all references allows the garbage collection to pick these PDDocument instances up and close and remove them. This pulls away the data underneath the pages copied into the PDDocument doc, resulting in the error you observe when trying to save doc.

To prevent this either clone the page objects into doc before adding them or keep all these temporary documents open until you save doc.

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