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

srujancherr
Tera Contributor

Hi @Muhammad Salar ,

srujancherr_0-1750253488659.png

 still same error

Hello, 
1. same script working for me with same code from knowledge article body, try for that and check for any changes for any other source.
2. Please check if you are using .doc not .docx is script include becasue in your error, i still see .docx

srujancherr
Tera Contributor

Hi @Muhammad Salar ,

 

Thank, yes its working now.

 

Instead of writing html code directly in here, we have a sn_doc_html_template table, there i created a record with my html content, can i call that record and convert to doc, can you please help me on this

Hello,
Try changing the table in query
and field values here 

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>';

like gr.getValue("text") is the main body here, it should work
OR
how are you coverting into pdf? any code
What is field type?

srujancherr
Tera Contributor

this is the UI Action code

var recordId = current.sys_id.toString();
var documentTemplateId = 'b473cc60838aaa10fba8c4e0deaad323';
var pdfName = current.number;
new sn_doc.GenerateDocumentAPI().generateDocumentForTask(recordId, documentTemplateId, pdfName);
action.setRedirectURL(current);

this is script include,

var GenerateDocumentAPI = Class.create();
GenerateDocumentAPI.prototype = Object.extendsObject(GenerateDocumentAPISNC, {

    initialize: function() {
        GenerateDocumentAPISNC.prototype.initialize.apply(this, arguments);
    },

    type: 'GenerateDocumentAPI'
});