If you want to create a button in a Model-Driven App to download a Word template in different formats (DOCX and PDF), follow these steps:
1. Create a Power Automate flow with an HTTP trigger.
2.Add a step for Word template population.
3. After that check if you want it to be with docx format or pdf
4. Create new file for the populated word template on SharePoint as shown below
5. If you want to convert the file to PDF add the below step then create new file at the SharePoint from the word template content.
6. Get the file content from SharePoint and pass the values to get content step and pass the content to variable with the demonstrated expression
7. Finally pass the variable to the response of the flow
7. You can use the below JS code to download the File returned from the Power Automate Flow
function CallPrintFlow(flowURL, ids, FileType) {
(async () => {
const rawResponse = await fetch(flowURL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(
{
id: ids,
FileType: FileType
})
}).then(response => response.text())
.then(data => {
//_base64ToArrayBuffer(data);
DownloadPDF(data, FileType);
}).catch(Error => {
Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");
//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
Xrm.Utility.closeProgressIndicator();
});
})();
} else {
console.log(this.responseText);
Xrm.Page.ui.setFormNotification("An Error occurred generating the PDF document, please contact support if the issue persists,code: " + err.message, "ERROR", "pdferror");
//Xrm.Page.ui.setFormNotification("An Error occurred generating the word document, please contact support if the issue persists,code: " + this.status, "ERROR", "worderror");
/*-----------STOP PROGRESS BAR [FAILURE]-----------*/
Xrm.Utility.closeProgressIndicator();
}
}
};
req.send();
}
function DownloadPDF(data,fileType) {
let str = data;
if (fileType == "PDF")
var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".pdf";
else if (fileType == "Word")
var filename = Xrm.Page.getAttribute("ldv_name").getValue().replace('/', '_') + ".docx";
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = str;
a.download = filename;
a.click();
Xrm.Utility.closeProgressIndicator();
}
If this post helps you with your problem, please give it a Thumbs Up.