You may be familiar with the fact that, within the ServiceNow HR application, there is a method to create PDF’s based on generated or user-fed content. You may not be aware of the fact that there is a way to create these PDF’s outside of the scope of HR – as a matter of fact, they can be created anywhere you have access to server-side scripting, be it a workflow, business rule, or script include. This utilizes the largely unadvertised GeneralPDF library.
This library will allow you to create an HTML-based PDF document and attach it as a file to a record on a table. An example of how you may format a script using this library would be:
var document = null;
var pdfDoc = new GeneralPDF.Document(null, null, null, null, null, null);
document = new GeneralPDF(pdfDoc, null, null);
document.startHTMLParser();
document.addHTML(YourHTMLObjectHere);
document.stopHTMLParser();
var att = new GeneralPDF.Attachment();
att.setTableName(tableName);
att.setTableId(sysIdOfYourRecord);
att.setName(yourFilename);
att.setType('application/pdf');
att.setBody(document.get());
GeneralPDF.attach(att);
Naturally, there are a few pieces of input that you need to be aware of:
TableName – This should be the actual name of the table you want to attach the PDF to. Examples: sys_user, sys_user_group, incident, change_request, etc.
SysIdOfYourRecord – The actual sys_id of the record you want to attach this PDF to.
yourFilename – The filename you would like the PDF to have.
yourHTMLObjectHere – This is the actual content of the PDF. This should be in plaintext HTML that can be interpreted. For example, you might define this variable as “p class = ‘myPTag’This is something I want in this document! It is VERY important! Here is an unrelated table
something | something else |
another row! | Another table cell! |
“.
This of course allows the utmost flexibility when creating your PDFs, as anything you can do in HTML you can do here.
Please be aware that you can also reference and display images in your PDF, as long as they are hosted in your ServiceNow instance in the “images” module. For that, a tag of img src = "/my_image.png"
should suffice, which will need to be included in your HTML variable in order to be displayed. This can lead to very professional and dynamic PDFs. That said, as of the latest version of ServiceNow, this is no longer the only way to create PDF’s within ServiceNow.
Quebec has introduced a new way to create your PDFs using a method called the Document API. Using the Document API, you create the contents of your PDF without needing any knowledge of HTML. You could make the argument it gives you greater control over the contents of your documents as well, as you can specify page breaks, page sizes/orientation (Such as Portrait vs Landscape) and meta data such as the author of the document.
An example of how you’d instantiate a Document API object:
var myPageSize = new sn_pdfgeneratorutils.PdfPage("LETTER");
var document = new sn_pdfgeneratorutils.Document.createDocument(myPageSize);
After initializing your object, you can begin creating content for your PDF. You can use the Document API’s Image method, Paragraph method, and Style method as an example to structure your content. You can create tables similar to excel using the Cell method. The Document API has enough content that it could be an article in itself and will likely be featured in the future.