Update 12 October 2021: Microsoft have stated that neither the organisation request described below, nor the publicly documented 'exportpdf' API are intended for public use, and that this public document (docs.microsoft.com/en-us/dynamics365/sales-enterprise/developer/exportpdf-api) is soon to be removed.
In October 2021, customers using the Organisation request from within a Plugin or Workflow started to see this error message:
“The private ExportPdfDocument request cannot be called from this plug-in because it has a different solution publisher."
This change was then reverted by Microsoft the following weekend in order to give customers time to find an alternative solution to this very common requirement.
If you think that |Microsoft should provide a publicly supported way of generating PDF's from Word Templates without having to first create a Word Document and save it in CRM, consider upvoting this idea:
Microsoft Idea · Make 'ExportPdfDocument' organisation request and 'exportPdf ' API action public (dynamics.com)
A common requirement for users of Dynamics 365 is to generate PDF documents based on Word Templates. A common solution to this is to use Power Automate to generate the PDF and there are plenty of examples online on how to do this, such as this. A disadvantage of this approach, however, is that the documents are created asynchronously and this means that users may have to wait an indeterminate amount of time, without feedback, before the Flow completes.
This is fine when the document is created as part of an automated process, such as overnight invoice generation, but not ideal when the requirement is to produce the document on demand.
Fortunately, since version 9.0.1905.2010 of Dynamics 365, an out-of-the-box method of generating PDF's from Word Templates has been available for the platform. Orginally announced for the Sales Hub, and only available for Account, Contact, Lead, Opportunity, Order, Quote, and Invoice tables, as of 2020 release wave 2 this has now been extended to all custom entities. (docs.microsoft.com/en-us/dynamics365/sales-enterprise/create-quote-pdf). At the moment, the ability to generate a PDF from an out-of-the-box form button is still a Sales Hub only feature, but there is, however, an OrganizationRequest (ExportPdfDocument) which can be used to generate a PDF from a Template.
The ExportPdfDocument request requires three parameters:
Thus we can create a method such as the following which takes the GUID of the relevant record and either the name or the GUID of the Word Template, and either the name or the ObjectType code of the relevant entity (with a couple of helper methods to retrieve the Guid of the Document and the ObjectTypeCode if not known):
The function returns the PDF as a ByteArray. What we do with this depends on our requirements. One such requirement might be to save the document as an annotation against the record the PDF was generated againts. This is easily done:
The exportPdf method is also available as a Web API Action. More details here: