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

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

srujancherr
Tera Contributor

Thanks @Muhammad Salar 

srujancherr
Tera Contributor

Hi @Muhammad Salar ,

 

If In HTML Template, I want to map one of the field, in HTML Template I added ${u_introduction_clause} this as a mapping to incident record, but when i click on button it's not getting the value from the record, can you please help me on this.

Hi @srujancherr 
Can you show the script for better understanding?

I used same script you suggested