π Download Word File from HTML Report (PL/SQL Dynamic Content) in Oracle APEX using JavaScript Library
This guide helps you integrate a "Download Word" feature in your Oracle APEX application using html2pdf.js.
-
Download the html2pdf.bundle.min.js library: π https://ekoopmans.github.io/html2pdf.js/
-
In Oracle APEX:
- Go to Shared Components β Static Application Files
- Click Upload File
- Upload the file html2pdf.bundle.min.js
- #APP_FILES#html2pdf.bundle.min.js
- Add This Static File to Page HTML Header.
<script src="#APP_FILES#html2pdf.bundle.min.js"></script>- Go to the page where you want the export feature.
- Under Page Attributes, scroll to Function and Global Variable Declaration.
- Paste the following code:
function exportHTMLToWord(elementId, filename) {
var header = `
<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
xmlns='http://www.w3.org/TR/REC-html40'>
<head><meta charset='utf-8'></head><body>`;
var footer = "</body></html>";
var content = document.getElementById(elementId).innerHTML;
var html = header + content + footer;
var blob = new Blob(['\ufeff', html], {
type: 'application/msword'
});
// Default file name
var docName = filename || 'Invoice.doc';
// Create download link
var link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = docName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
-
Create a Button
- Label it (e.g.,
Download Word) - Position it on the same APEX page as your report (HTML container with
printID).
- Label it (e.g.,
-
Create a Dynamic Action
- Event:
Click - Selection Type:
Button - Button: Select the button you just created (e.g.,
Download Word)
- Event:
-
Add a True Action
- Action Type:
Execute JavaScript Code - Code:
exportHTMLToWord('printID', 'Invoice-' + new Date().toISOString().slice(0,10) + '.doc');
- Action Type:
html2pdf.jsmust be loaded on the page (see section 2 of the documentation for CDN or static file setup).- The
exportHTMLToWordfunction must be declared globally (in Page > JavaScript > Function and Global Variable Declaration). - Create a Dynamic Action for the buttonβs click event to call the JavaScript function.