I'm trying to populate a few thousands of PDF forms with around 100 fields per file. In Python it looks like this:
def createPDF(data) :
import winerror
from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT
ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL)
template = 'C:\\template.pdf'
for row in data:
app = Dispatch('AcroExch.App')
pdfDoc = Dispatch('AcroExch.PDDoc')
pdfDoc.Open(template)
jso = pdfDoc.GetJSObject()
jso.getField('field1').Value = row[0]
#...
jso.getField('field100').Value = row[99]
pdfDoc.Save (33, row[0] + '.pdf')
pdfDoc.close()
app.CloseAllDocs
app.exit
This is some very simplified version of the code. I know it can be faster with opening and closing 'AcroExch.App' outside the loop, safer with using try... but for this example it really does not matter.
It works quite good for small batches as it populates around 200 files per minute. Unfortunately with every additional file it starts to slowing down until crash with error like this:
AttributeError: Property '<unknown>.Value' can not be set
so in some moment it cannot populate anymore fields.
When it is running I see in task manager that Acrobat is using more and more memory, but those are some small values: it starts from 50MB, goes to arund 900MB and crashes when there is still a few GB of free memory. It really looks like Acrobat is leaving some trash that is slowing and crashing it down.
In "Interapplication Communication withthe Acrobat SDK" I see that AcroExch.AVDoc.close function after closing file is allowing to reuse it ("To reuse an AVDoc object, close it with AVDoc.Close, then use the AVDoc object’s LPDISPATCH for AVDoc.OpenInWindow"), but in the descrition of other functions that I am using: AcroExch.PDDoc.close and AcroExch.App.CloseAllDocs there is noting about leaving something to reuse.
I have this problem in Windows 10 Enerprise with using current Acrobat DC Pro (32-bit) version. I was testing also some other acrobat versions and even running AcroExch.App from VBA, but results were the same.
At the moment I have one workaround for this. On error and after some N created files(based on number of fields in a form) it is:
- closing Acrobat with AcroExch.App.exit
- wait around 15 or 20 seconds before opening it once again
This additional time allows system to close Acrobat app and clear all garbages from memory, but waiting 20 second every few minutes is not too efficient.
Do you maybe know some other way how it can be resolved? Something like forcing Actobat to clear cache without closing it or maybe just using some different functions to populate forms?