Before I start, I want to talk about one issue that I’ve faced many times while working in ServiceNow. Whenever I want to generate any custom-styled PDF document in ServiceNow, I have to look around a lot.
I am sure that you must be in the same boat for a-while. Many times, we try to use GeneralForm API
solution from the HR module or try to use an external document conversion solution(s). GeneralForm
API use iTextPDF v5
which is having many bugs while working with styled HTML to convert.
But no worries, ServiceNow has introduced Next-Gen PDF Generation & Conversion APIs
. These APIs are based on a newer release of iTextPDF v7, where it addresses critical flaws of the previous version such as page orientation & text formatting
.
Plugin
com.snc.apppdfgenerator
is activated by default in Quebec
Download PDF Conversion Demo update-set for further steps as I have included all the artefacts that we are going to use in further discussion
Load this update set in your Quebec instance
Let’s see APIs in action
Conversion API
API group to convert HTML/SVG to PDF
HTML to PDF:
convertToPDF
➔ Convert HTML code to PDFconvertToPDFWithHeaderFooter
➔ Convert HTML to PDF with Header & Footer
Below is the code syntax to use this api:
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDF( html, <<table_name>>, <<sys_id_of_record_where_pdf_need_to_attach>>, <<pdf_fiile_name_without_.pdf_extension); gs.info(JSON.stringify(resultX));
Here inputs are:1st Parameter (html)
: HTML code to convert into PDF document2nd Parameter (table_name)
: Table name where generated PDF need to be attached3rd Parameter (sys_id)
: Record sys_id to attach generated PDF4th Parameter (pdf_file_name)
: File name for generated pdf (without .pdf extension)
The output consists below properties:attachment_id
: sys_id of the generated attachment filemessage
: Success or failure messagerequest_id
: sys_id of PDF conversion request recordstatus
: success or fail
To try out:
- Go to
PDF Conversion Demo
- Open
PDF Conversion Template
, Select recordPDFC0001002
and change the HTML template as per your own. Here I’ve included one sample HTML. - Open
PDF Conversion Demo Scripts
, OpenSample HTML to PDF
- Make changes in the table and record sys_id
- Hit
Run Fix Script
- Go to incident table (or your selected table) and open above record. You will see an attachment having name
my_smaple_pdf.pdf
. Download it and output should be something like below
Now, let’s see how can we have headers and footers in our generated PDF. To have desired output, we need to make few changes.
1st, we have to create object to hold header and footer data
var _map = new Object(); _map["HeaderImageAttachmentId"]="978f17a51bb22850abbdbb7c0a4bcb3e"; _map["HeaderImageAlignment"]="left"; _map["FooterImageAttachmentId"]="978f17a51bb22850abbdbb7c0a4bcb3e"; _map["FooterImageAlignment"]="top_left"; _map["FooterText"]="Creatde by Artemis"; _map["FooterTextAlignment"]="bottom_left"; _map["PageSize"]="A4"; _map["GeneratePageNumber"]="true"; _map["TopOrBottomMargin"]="75"; _map["LeftOrRightMargin"]="36"; _map["HeaderImageHeight"]="50";
In this, we are havingHeaderImageAttachmentId
/ FooterImageAttachmentId
: sys_id of attachment image to be used as headerHeaderImageAlignment
/ FooterImageAlignment
/ FooterTextAlignment
: Placement of header, values (left / right / center with combination with top/bottom like: top_left)FooterText
: Any footer textPageSize
: Page size, values (A4, LEGAL, LETTER)GeneratePageNumber
: Generate page number(can generate only numbers)TopOrBottomMargin
: Top and bottom margin, Default value 72LeftOrRightMargin
: Left and right margin, Default is 36HeaderImageHeight
: Customize header image height
All used attachment are included in PDF Conversion Template record
PDFC0001002
in update set
Code syntax to generate PDF with header and footer is:
var resultX = new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDFWithHeaderFooter( html, <<table_name>>, <<sys_id_of_record_where_pdf_need_to_attach>>, <<pdf_fiile_name_without_.pdf_extension>>, myobj);
To see it in action, use Sample HTML to PDF with Header n Footer
in PDF Conversion Demo Script
. Once run, output should be something like below:
Now let’s see how we can play with SVG content
SVG to PDF:
convertSVGToPDF
➔ Convert SVG image(xml code) code to PDFconvertSVGToPDFWithSize
➔ Convert SVG image(xml code) code to PDF with sizing parameteraddSVGToPDF
➔ add SVG image to PDF
Let’s understand SVG first. SVG define graphics in XML format. Every element and every attribute in SVG files can be animated. For our demo example, I’ve uploaded one sample svg on record PDFC0001002
.
To work wit h svg, we have to extract its XML content and use it in our script (just open svg in any editor and copy xml content, open record PDFC0001003
for sample).
Below is the code syntax for SVG to PDF conversion
var v = new sn_pdfgeneratorutils.SVGToPDFConversionAPI; var result = v.convertSVGToPDF( svg, <<pdf_fiile_name_without_.pdf_extension>>, <<table_name>>, <<sys_id_of_record_where_pdf_need_to_attach>> );
Here inputs are:1st Parameter (svg)
: SVG’s XML content to convert into PDF document2nd Parameter (pdf_file_name)
: File name for generated pdf (without .pdf extension)3rd Parameter (table_name)
: Record sys_id to attach generated PDF4th Parameter (sys_id)
: Record sys_id to attach generated PDF
To see it in action, use Sample HTML to PDF with SVG
in PDF Conversion Demo Script
. Once run, output should be something like below:
Rest method are self-explanatory. I would like you to get it done by your own. Do let me know in case you are stuck in these functions.
In the next post, we are gonna work with Utility API's
where once can work with fillable PDFs.