How to convert a non pdf document like word, excel or image to pdf in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 04:50 AM
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.
Result:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 04:57 AM
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);
},
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