PDF to Word conversion

srujancherr
Tera Contributor

Hi,

We are currently working on a proof of concept (POC) in our Personal Developer Instance (PDI) within ServiceNow. As part of this POC, we are generating a PDF file from an HTML Template and attaching it to the Risk Response Task table.

However, instead of a PDF, our requirement is to generate and attach a Word (DOCX) document. We would like to know if there is a native or recommended way in ServiceNow to convert a PDF file to a Word (DOCX) file.

To explore alternatives, we attempted two different approaches:

  1. Custom Azure-hosted Solution: We developed our own solution to convert PDFs to DOCX, hosted on Azure. While the integration with ServiceNow was successful, the resulting file was corrupted. Upon checking Azure logs, it appears that the file being received is already corrupted at the time of transmission.

  2. Open Source API (ConvertAPI): We also integrated ConvertAPI to achieve the conversion. Unfortunately, the result was the same — a corrupted Word document was returned.

Given these challenges, we are seeking guidance or a working solution to successfully convert and attach a Word (DOCX) file in the Risk Response Task record in ServiceNow.

We would greatly appreciate any support or recommendations you could provide.

2 ACCEPTED SOLUTIONS

Hello @srujancherr , These are simple things you can do this on your own by modifying it,
For images, i am not 100% sure. 
If your original query is answered then Mark helpul and Accept it as solution
Regards

View solution in original post

Hi @srujancherr 
You can check all attached variables in script and then set their values like this.
Just attach any incident values in template and use this script, it will dynamically change those variables into values.

getContentForWordTemp: function(temp, rec) {
        var tempGr = new GlideRecord('sn_doc_html_template');
        tempGr.addQuery('sys_id', temp);
        tempGr.query();

        while (tempGr.next()) {
            var text = tempGr.getValue('html_script_body');

            var recGr = new GlideRecord('incident');
            if (!recGr.get(rec)) {
                return;
            }

            var matches = text.match(/\$\{([^}]+)\}/g);
            if (matches) {
                for (var i = 0; i < matches.length; i++) {
                    var placeholder = matches[i];
                    var field = placeholder.replace('${', '').replace('}', '');
                    var value = recGr.getDisplayValue(field) || '';
                    var regex = new RegExp('\\$\\{' + field + '\\}', 'g');
                    text = text.replace(regex, value);
                }
            }

            var HtmlHead = "<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 content = HtmlHead;
            content += "<h1><strong>" + tempGr.getDisplayValue('name') + "</strong></h1><br>";
            content += "<div>" + text + "</div>";
            content += "</body></html>";

            var fileName = tempGr.getDisplayValue('name') + '.doc';
            var contentType = 'application/msword';

            var attachment = new GlideSysAttachment();
            var attachmentID = attachment.write(recGr, fileName, contentType, content);

        }
    },

View solution in original post

28 REPLIES 28

Maik Skoddow
Tera Patron
Tera Patron

Hi @srujancherr 

unfortunately, ServiceNow has no OOTB solution for converting file formats and your approach to leverage external services is the most promising one.

However, I would create the DOCX file directly from the HTML and CSS code and not request a conversion. Maybe this would return a correct DOCX file.

Maik

Muhammad Salar
Giga Sage

Hello, 
You can create word doc file like this, in case you need it

getHtml: function() {
        var list = []; 
        var sysId = this.getParameter('sys_id');
        var gr = new GlideRecord('kb_knowledge');
        gr.addQuery('sys_id', 'IN', sysId);
        gr.query();
        while (gr.next()) {
            var object = {};
            var fileName = gr.getDisplayValue('short_description') + '.docx';
            var contentType = 'text/plain';
            var text = gr.getDisplayValue('text');
            //var txtDesc = content.replace(/<\/?[^>]+(>|$)/g, ""); For converting html to string
			var HtmlHead = "<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>";

            //var content = HtmlHead + '<h1>' + gr.getDisplayValue('short_description') + '</h1>' + '<p>' + gr.getDisplayValue('number') + ' - Latest Version (' + gr.getDisplayValue('workflow_state') + ')</p>' + '<br>' + '<div>' + gr.getValue("text") + '</div>';
			
			var content = HtmlHead + '<h1>' + gr.getDisplayValue('short_description') + '</h1>' + '<p style="margin-top:-20px;">' + gr.getDisplayValue('number') + '<b>' + ' . ' + '</b>' + gr.getDisplayValue('version.version') + '<b>' + ' . ' + '</b>' + gr.published + '</p>' + '</p>' + '<br>' + '<div style="margin-top:-30px;">' + gr.getValue("text") + '</div>';
			
            var attachment = new GlideSysAttachment();
            var agr = attachment.write(gr, fileName, contentType, content);
            object.ID = agr;
            list.push(object);

        }
        return JSON.stringify(list);
    },

 

var ga = new GlideAjax('exportToDoc');
    ga.addParam('sysparm_name', 'getHtml');
    ga.addParam('sys_id', sysId);
    ga.getXML(getRequired);

    function getRequired(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var json = JSON.parse(answer);
        for (var i = 0; i < json.length; i++) {
            top.window.open('/sys_attachment.do?sys_id=' + json[i].ID);
        }
    }

 

Hi @Muhammad Salar ,

Thankyou for your reply,

Code is working fine, word document is creating but when i'm opening the word document getting below error

srujancherr_0-1750252160021.png

 

Hi @srujancherr , yes there is a slight mistake
replace this in script include 

 var fileName = gr.getDisplayValue('short_description') + '.docx';
to
 var fileName = gr.getDisplayValue('short_description') + '.doc';

Try and accept this as solution, if your requirement is fulfilled