WebViewer Version: @pdftron/webviewer@10.6.0-20240103 (as per Can't donwload with template generation variables)
Do you have an issue with a specific file(s)? No
Can you reproduce using one of our samples or online demos? Yes
Are you using the WebViewer server? No
Does the issue only happen on certain browsers? No
Is your issue related to a front-end framework? No
Is your issue related to annotations? No
Please give a brief summary of your issue:
Please describe your issue and provide steps to reproduce it:
I am using the template functionality to generate PDFs from a word doc that may be 20-30+ pages in length using the WebViewer. The issue that I have noted (using the ESM workers) is that calling applyTemplateValues in this context and then subsequently calling getFileData immediately seems to only receive some of the page data and therefore hangs (it appears as though they are split up for processing and subsequently loaded back in after attaching some logging to the events). This is the case even if calling getDocumentCompletePromise (this just seems to wait for the first document “chunk” to become available).
I have been able to replicate this by loading the attached file in this online sample: Document Generation using Text/Image Template With JS Demo | Apryse WebViewer and running the following code in the console:
const instance = WebViewer.getInstance();
const doc = await instance.Core.documentViewer.getDocument();
await doc.getDocumentCompletePromise();
await doc.applyTemplateValues({
Date: "01/01/2024",
applicant_first_name: "Test",
applicant_surname: "Person",
company_name: "Company",
dest_street_address: "Address",
logo: "None",
sender_name: "Sender",
table: "Table"
});
await doc.getDocumentCompletePromise();
const xfdfString = await instance.Core.annotationManager.exportAnnotations();
const data = await doc.getFileData({
xfdfString,
downloadType: 'pdf',
});
const blob = new Blob([data], { type: 'application/ pdf' });
window.open(URL.createObjectURL(blob));
Adding the following line immediately after applyTemplateValues seems to allow enough time for this processing to finish, which resolves the issue:
await new Promise((resolve) => setTimeout(resolve, 1000));
As a temporary work around, I have found that attaching an event to the “pagesUpdated” event on the document viewer allows me to check the “contentChanged” array length against the document’s page count, although this is not entirely ideal as we have instances of templates where the page count may change by a page once values are applied. I’m also not entirely confident that this will always be the case - it’s just a behaviour I have observed while debugging.
Is there a more reliable way to do this that I’m missing? Look forward to hearing from you.
letter_template (1).docx (28.3 KB)