The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to convert a non pdf document like word, excel or image to pdf in ServiceNow

Srishti14
Tera Contributor

Hello Everyone,

I'm working on a requirement where I need to convert a non pdf document attached to a variable to pdf and attach that pdf to ritm generated after raising the request.When I'm trying below script it is creating an attachment id which is a pdf document but on opening the document the content is missing.it shows undefined.Can anyone please help me resolving this issue.

Script: Business rule on Requested item table after insert.

(function executeRule(current, previous /*null when async*/ ) {

var id = current.variables.test_upload;
var sysId = current.sys_id;
var v = new sn_pdfgeneratorutils.PDFGenerationAPI;

var gr = new GlideRecord("sys_attachment");
var html;

if (gr.get("id")) {
html = gr.file_name.toString();
}

var result = v.convertToPDF(html, "sys_attachment", id, "invoice");
var string = JSON.stringify(result);
var parse = JSON.parse(string);
var sys = parse.attachment_id;
gs.log('attachment id is' + sys);
var grattach = GlideSysAttachment.copy('sys_attachment', sys, 'sc_req_item', sysId);
})(current, previous);

Result:

Srishti14_0-1708433394229.png

 

 

1 REPLY 1

Community Alums
Not applicable

Hi @Srishti14 ,

OOTB, there is something called PDF Connector available but it comes with additional licensing.

However, as a custom solution,

Make sure these 2 plugins are activated:

PDF Generator (com.snc.pdf_generator) 

WebKit HTML to PDF (com.snc.whtp)

Try with this one:

var PDF = Class.create();

PDF.prototype = Object.extendsObject(AbstractAjaxProcessor, 

  _document : null, 

  example : function (){

  this.createPDF('<p>Hello World!</p>',   //HTML to convert to PDF

    'incident',   //Attach PDF to table

    'd324db7d2b9aa20072675aa419da1547', //Attach PDF to record/sys_id

    'myFile.pdf');   //attachment filename

  },

  createPDF : function(html, table, sys_id, filename) {   var pdfDoc = new GeneralPDF.Document(null, null, null, null, null, null);

  this._document = new GeneralPDF(pdfDoc);

    this._document.startHTMLParser();

    this._document.addHTML(html);

  this._document.stopHTMLParser();

    this._saveAs(table, sys_id, filename);

  },

  •   _saveAs : function (table, sys_id, filename){

    var att = new GeneralPDF.Attachment();

    att.setTableName(table);

    att.setTableId(sys_id);

    att.setName(filename);

    att.setType('application/pdf');

    att.setBody(this._document.get());

    GeneralPDF.attach(att);

  },

      type: 'PDF'

});

This script include will convert the doc to pdf via code.

Also, You'll likely need to develop an integration and leverage a third party API to convert a document. Something like https://www.convertapi.com/doc-to-pdf