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.

Generate PDF with Header Image

karthikdurga
Mega Expert

I have a requirement to generate pdf and attach it to a record. I've used OOB GeneralPDF script include and achieved that (code below). However, I am unable to pass header image and footer text. Any suggestions on how to pass header/footer ? Thanks in advance.

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'  

});

5 REPLIES 5

Patrick Fedigan
Giga Guru

Hello Karthik,



Seems to me that it is only passing in HTML information for the PDF. I would create a HTML header and HTML footer to append to the front and back of your HTML <p>Hello World!</p> message.



You can create an HTML header with inline styling like this example below:



<div class="body" style="font-family: Helvetica,Arial,sans-serif; font-size: 15px;">


<div class="topnav" style="background-color: #2d4c61; padding: 5px; float: left; width: 100%; color: #ffffff; verticle-align: middle;">


<div style="margin: 13px; display: inline-block; vertical-align: top;">Company</div></div></div>



Hope this helps.



Cheers,



Patrick


Hi Patrick



I tried passing the header and footer in the html but it doesn't print on every page. I looked at the script include GeneralPDF and has a variable this.headerimage. However, when I pass the image link it doesn't show up on PDF. Please let me know if you have any other ideas. Thanks for your response.


Hi Karthik,



Did you find a solution for this? I`m facing the same issue.


If you have a personal instance, enable the HR module and check how it leverages the PDF creation there using templates and other artifacts.