Search code examples
javascriptpdfservicenow

How convert template HTML with images to PDF in ServiceNow?


I need to create dynamic pdf with html templates in servicenow, but my problem is that these pdf must contain images and styles, and I have not been able to solve it.

try using the api of GeneralPDF of servicenow and get the template converted to pdf but only when it contains text. when I put images I get the following error:

This error appears to me when executing my code:

ExceptionConverter: java.io.IOException: The document has no pages.: org.mozilla.javascript.JavaScriptException: ExceptionConverter: java.io.IOException: The document has no pages.:

this is in a script include and is called from UI Action my code to convert the html to pdf is the following:

create : function (sys_id){ 

   var carta = new GlideRecord('x_solsa_casos_plant_doc');
    carta.addQuery('sys_id','6f1e4ac8db29f300ab7c0f95ca96197a');
    carta.query();
    if(carta.next()){
        var parsedBody = carta.body;
        var gr = new GlideRecord('x_solsa_casos_x_solsa_casos');
        gr.get('sys_id',sys_id);
        var sampleString=parsedBody.toString();
        var reg = new SNC.Regex('/\\$\\{(.*?)\\}/i');
        var match = reg.match(sampleString);
        var count =0;
        var variables = [];
        var values = [];
        var tmpValue;
        while (match != null) 
        {
            variables.push(match.toString().substring(match.toString().indexOf(',')+1));
            match = reg.match();
            values.push(variables[count]);
            gs.log("array values : " + values);
            if(gr.getDisplayValue(values[count])==null || JSUtil.nil(gr.getDisplayValue(values[count])))
            {
                tmpValue='';
            }else{
                tmpValue=gr.getDisplayValue(values[count]);
                gs.log("tmpValue :" +tmpValue);
            }
            parsedBody = parsedBody.replace('${'+variables[count]+'}', tmpValue);
                count++;
                gs.log("parsedBody : " + parsedBody);
        }
        this.createPDF(parsedBody,'x_solsa_casos_x_solsa_casos',sys_id,'carta.pdf');  
    }

},

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

},


Solution

  • Looks like parsedBody is empty or doesn't always contain HTML. According to this answer, paseXHtml (which ServiceNow probably uses and should be in the complete stack trace) expects HTML tags, not just text: https://stackoverflow.com/a/20902124/2157581