Export to MS-Word with styling

By: Carlo Santos | Asked: 09/23/2023
ForumsCategory: Code HelpExport to MS-Word with styling
Carlo Santos asked 10 months ago
I have a script from https://www.codexworld.com/export-html-to-word-doc-docx-using-javascript/ that will allow me to export a view/post into a Word document. Does anyone know how to style the export to specify the font family and size instead of using the default word settings? This is the script:
function Export2Word(element, filename = ''){
    var preHtml = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='https://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML To Doc</title></head><body>";
    var postHtml = "</body></html>";
    var html = preHtml+document.getElementById(element).innerHTML+postHtml;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });
    
    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
    
    // Specify file name
    filename = filename?filename+'.doc':'document.doc';
    
    // Create download link element
    var downloadLink = document.createElement("a");

    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = url;
        
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    
    document.body.removeChild(downloadLink);
}
1 Answers
Victor Font answered 10 months ago
This has nothing to do with Formidable. I suggest you ask the developer that wrote your snippet.
Carlo Santos replied 10 months ago

Sorry, I thought because it was a View setting that could be adjusted but I see your point. Thanks Victor.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right