web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Can I create a documen...
Power Apps
Unanswered

Can I create a document from a template in JavaScript?

(0) ShareShare
ReportReport
Posted on by 311

In my model-driven app, I have some Word document templates setup in the Power Apps Templates feature.  I would like to be able to create a document from one of the templates triggered by JavaScript code.  The new document would be populated with values for a specified record.  Ultimately, I would have the document returned in the code so I can place it in a folder.  Is this possible?

I have the same question (0)
  • Verified answer
    jm125245 Profile Picture
    on at

    You can use the exportPdf Action. Here's some example TypeScript code that will return a File object - for uploading, saving, printing, etc.

     

     

     async function exportPDF(entityRef: Xrm.LookupValue, templateRef: Xrm.LookupValue, primaryEntityTypeCode: number, name: string): Promise<File> {
     const r = await Xrm.WebApi.online.execute(new ExportPDFRequest(
     primaryEntityTypeCode,
     templateRef.id,
     entityRef.id,
     ));
    
     if (!r.ok) {
     throw Error(`"Response not ok: ${r.status} ${r.statusText}`);
     }
    
     const rBody = <{ PdfFile: string }>await r.json();
     const pdfFile = new File([base64ToByteArray(rBody.PdfFile)], `${name}.pdf`, { type: 'application/pdf' });
    
     return pdfFile;
     }
    
     function base64ToByteArray(base64: string) {
     return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
     }
    
     class ExportPDFRequest {
     public EntityTypeCode: number
     public SelectedTemplate: object;
     public SelectedRecords: string;
    
     constructor(
     entityTypeCode: number,
     documentTemplateID: string,
     selectedRecords: string,
     ) {
     this.EntityTypeCode = entityTypeCode;
     this.SelectedTemplate = {
     "@odata.type": "Microsoft.Dynamics.CRM.documenttemplate",
     documenttemplateid: documentTemplateID,
     };
    
     this.SelectedRecords = JSON.stringify([selectedRecords]);
     }
    
     public getMetadata() {
     return {
     boundParameter: null, // not bound
     parameterTypes: {
     EntityTypeCode: {
     typeName: 'Edm.Int32',
     structuralProperty: 1, // PrimitiveType
     },
     SelectedTemplate: {
     structuralProperty: 5, // Entity Type - don't validate just pass as object
     },
     SelectedRecords: {
     typeName: 'Edm.String',
     structuralProperty: 1, // PrimitiveType
     },
     },
     operationType: 0, // action
     operationName: "ExportPdfDocument"
     };
     }
     }

     

     

    You could also consider calling the same API from Power Automate -> Generate PDF from Document Templates in CDS & Dynamics 365 CE using native Web API with Flow - There's Something About Dynamics 365 (2die4it.com)

  • vdblohowiak Profile Picture
    Microsoft Employee on at

    You rule man. The only non-plugin solution of ExportPdfDocument I could find on the interwebz. Even works from within a PCF control with a little bit of tweaking. I wish I could add the code here but my post is failing to submit when I include it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard