Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to generate custom pdf from a variable which is holding html content (body)

jodha231
Kilo Contributor

Hello,

How to create a pdf from a variable which is holding html content(body)?

Scenario - 

Need to get Invoice generated when purchase order is ordered. Its a customised pdf and it goes out to vendor alongwith similar notification. I achieved exact formatting and fields needed from multiple tables in notification and notification works fine.

I also achieved fetching body (html content) of this notification email after it is sent and processed for particular PO via a BR.

Now all I need to print this body in pdf file and attach it to purchase order through this BR.

I am unable to find much documentation on how to use itextpdf, generalpdf script includes available in ITSM platform. Any help is appreciated.

*Snow Hi team was not much of help, putting this out here if some expert can help. I already have html content, just stuck as to how to print it.

 

2 REPLIES 2

jodha231
Kilo Contributor

Update - 

I wrote the below code to generate pdf and this code works fine and attaches pdf to record; with simple html content when i call from client script and pass static html value like -

<p>Hello World <\p>

But when i am passing my derived 'body' via a variable in script include it fails. The 'body' in email log table is String full utf8 field with 8000 limit and when i am holding this value in my variable and passing to script include it fails.

any pointers how to keep value static as its fairly long and how to pass it cleanly?

Below Script Include Code - 

var PDF = Class.create();
PDF.prototype = Object.extendsObject(AbstractAjaxProcessor, {

_document : null,

genpdf : function (){

//var body = "<p> Hello World <p>";

var body = '';
var table = this.getParameter('sysparm_table');
var record = this.getParameter('sysparm_record');
var sys_id = this.getParameter('sysparm_sys_id');
var filename = record+'.pdf';

var gr = new GlideRecord('sys_email_log');
gr.addQuery('notification','unique id of notification');
gr.addQuery('email.instance', sys_id);
gr.query();

while(gr.next()){
state = gr.email.state;
if (state != 'processed'){
gs.sleep(300000);}
else{
body = gr.email.body.toString();
}
}

return this.createPDF(body, //HTML to convert to PDF

table, //Attach PDF to table

sys_id, //Attach PDF to record/sys_id

filename); //attachment filename

},

createPDF : function(body, 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(body);

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'

});

Rahmati1
Tera Contributor

Where to write this code in Email Inbound Action ?