Issue with applyTemplateValues on large docx documents

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)

Hi there,

Thank you for contacting WebViewer forums,

It looks like with the above code, I am able to successfully get all 20 pages loaded into the viewer with the template variables filled in if I remove the 'downloadTyper: ‘pdf’ in getFileData method

const doc = await instance.Core.documentViewer.getDocument();

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"
});

console.log('templateValues applied')

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));

Alternatively, you can forgo the getFileData method completely by loading the PDF after the template values are applied:

Best regards,
Kevin Kim

Hi Kim,

Thanks for your reply; I can confirm that the viewer is updated with all of the pages, however this is not reflected in the file generated by getFileData; when generated using the online example and the supplied document, this results in a docx document with none of the values filled. As the use case is to apply the template values and then extract the generated PDF (for upload), I’ll have to continue using the work around for now (unless there’s another way to achieve this).

Thanks for the quick reply!

Look forward to hearing from you soon.

Hi there,

Thank you for your response,

We have added a backlog for this issue, we don’t have an expected timeline but please keep an eye out for the changelogs for when this gets added in.

Best regards,
Kevin Kim

Hi there,

A fix for this issue has been completed and will be available in our nightly experimental build within the next 24 hours. You can download the latest experimental build at Apryse Developer Portal.
This build is meant for testing and not recommended for production use.

Best regards,
Kevin Kim

Thanks again for the fix on this one guys :slight_smile:

There is another issue that I have noticed now that we have developed our initial prototype solution and have been trialling this with customers.

The issue relates to the document’s scroll position resetting in the same circumstances that this bug originally appeared, which I believe is related to how the PDF / document data is actually processed in the WebViewer engine (i.e. it seems to batch up 6-7 pages at a time for processing)

If you could have a look into this (or let me know if a new support ticket will be required) that would be fantastic!

Hi Jake,

Could you create a new ticket at our support channel: Apryse
And include as much relevant information as you can provide, thank you.

Best regards,
Kevin Kim