Export to PDF directly from portal

Exporting data or web pages to PDF is a powerful feature in Power Pages, especially for use cases like:

  • Invoices or receipts
  • Form submissions (application forms, registrations)
  • Certificates or reports
  • Print-friendly documentation

Since Power Pages runs in the browser and is part of the Power Platform ecosystem, generating PDFs involves combining JavaScript, HTML templates, and optionally Power Automate or Azure Functions for more advanced cases.

This guide covers:

  • PDF generation approaches (client-side & server-side)
  • Step-by-step implementation
  • Formatting best practices
  • Security considerations

Approaches for PDF Generation

ApproachTools UsedWhen to Use
Client-sideJavaScript (jsPDF, html2canvas)Light exports (forms, static content)
Server-sidePower Automate, Power Apps, Azure FunctionsComplex reports, database exports
Third-party servicePDFShift, PDFCrowd, etc.Advanced rendering, watermarking

Client-Side PDF Export using jsPDF + html2canvas

Step 1: Add Libraries to Your Page

  1. Go to Power Pages Portal Studio
  2. Open the page or form where you want to enable PDF export
  3. Insert the following in the HTML section or Web Template:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>

Step 2: Add the HTML Element to Export

Wrap the content you want to export inside a container:

<div id="pdfContent">
<h2>Invoice #12345</h2>
<p>Date: 2025-04-23</p>
<p>Name: John Doe</p>
<p>Total: $320.00</p>
</div>

Step 3: Add the Export Button

<button onclick="generatePDF()">Download PDF</button>

Step 4: Write the PDF Script

Add this JavaScript block:

<script>
async function generatePDF() {
const { jsPDF } = window.jspdf;
const content = document.getElementById("pdfContent");

const canvas = await html2canvas(content, { scale: 2 });
const imgData = canvas.toDataURL("image/png");

const pdf = new jsPDF({
orientation: "p",
unit: "mm",
format: "a4"
});

const imgProps = pdf.getImageProperties(imgData);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;

pdf.addImage(imgData, "PNG", 0, 0, pdfWidth, pdfHeight);
pdf.save("document.pdf");
}
</script>

This code converts the selected HTML to an image and embeds it in a downloadable PDF file.


Limitations of Client-Side PDF

LimitationNotes
May struggle with large contentBetter for 1-2 pages of static content
No background images/stylesCSS2/3 limitations
No server-side processingCannot pull external data at export time

Server-Side PDF with Power Automate + OneDrive/Word

If you’re working with dynamic data (Dataverse or SharePoint) or need templated documents, use Power Automate:


Step 1: Create a Word Template

  1. Open Word
  2. Design your template using Content Controls (Developer tab)
    • Example: <<CustomerName>>, <<TotalAmount>>
  3. Save to OneDrive or SharePoint

Step 2: Build Power Automate Flow

  1. Trigger: When HTTP request is received
  2. Actions:
    • Get data from Dataverse or Power Pages form
    • Populate Word Template
    • Convert Word to PDF
    • Return PDF file as response
  3. Enable flow to receive input parameters

Step 3: Call Flow from Power Pages

Use JavaScript fetch or Web API call:

fetch("https://prod-xx.azure-api.net/pdf-flow", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ recordId: "12345" })
})
.then(res => res.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = "report.pdf";
document.body.appendChild(a);
a.click();
a.remove();
});

Bonus: Embed PDF Viewer Instead of Download

Sometimes you may want to preview the PDF before download:

<iframe id="pdfPreview" style="width:100%; height:600px;"></iframe>
// Insert blob into iframe for preview
pdfBlob = await fetch(...).then(res => res.blob());
document.getElementById("pdfPreview").src = URL.createObjectURL(pdfBlob);

Security & Performance Tips

TipWhy
Sanitize dynamic contentAvoid XSS in PDF
Limit large PDF exportsEspecially client-side
Use server-side for complex docsBetter formatting and control
Use HTTPSAvoid blocked scripts and mixed content
Avoid exposing full record IDsSecure API endpoints and data access

Use Cases in Real-World Scenarios

Use CaseMethod
Certificate generationPower Automate + Word template
Invoice downloadClient-side jsPDF
Report exportsServer-side PDF with dynamic data
Form summary downloadhtml2canvas for HTML capture

Leave a Reply

Your email address will not be published. Required fields are marked *