Generating a PDF from HTML
Import
·
Feb 19, 2020
·
article
This is a brief guide to using the GeneralFormAPI Script Include to generate a PDF from an HTML source. There are a few utility classes for generating PDFs but ultimately they all end up in GeneralFormJava, which is a set of wrapper classes for iText 5.5.x. The limitations to what we can do with PDF are imposed by those wrapper classes.
Some things to consider:
- Only supports generating a PDF into a new attachment, so you will need a record to send the output to.
- A limited set of HTML and styling is supported.
- A single image can be used in the page header.
- A single image or plain text annotation can be used in the page footer.
- Images need to be in (?) png format and are supplied using a data URI (i.e. base64 encoded).
- You cannot use
in your HTML: if you're using data from HTML fields make sure you filter out
before passing to createPDF. - I haven't tested SVG (see createPDF in GeneralFormAPI).
- Use HTML tables to achieve layout (but using width= will break)
- For more limitations see https://hi.service-now.com/kb%5Fview.do?sysparm%5Farticle=KB0693303
The following is a complete example that should work when executed as a background script. Only do this in a sub-production instance!
(function() {
// grab a random RITM
var ritm = new GlideRecord('sc_req_item');
ritm.setLimit(1);
ritm.query();
ritm.next();
var filename = ritm.number + '.pdf';
gs.debug('Writing PDF to ' + ritm.number);
var table = ritm.getTableName();
var table_sys_id = ritm.getUniqueValue();
// create a new PDF generator
var formAPI = new global.GeneralFormAPI(filename, table, table_sys_id);
// grab a random image
var headerImage = null;
var att = new GlideRecord('sys_attachment');
att.addQuery('content_type', 'image/png');
att.addEncodedQuery('table_nameISNOTEMPTY');
att.query();
if (att.next()) {
headerImage = getAttachmentBase64(att);
}
var footer = 'Your footer message here';
// setDocument(header image, footer image, footer text, header alignment, footer alignment, paper size)
// alignment = "0" = left, "1" = centre, "2" = right. An invalid alignment will cause the image to not appear
formAPI.setDocument(headerImage, null, footer, '1', '1', 'a4');
// source HTML
// must be well-formed XML and not use <hr> (really)
var pages = [
{heading: '<h1>My First PDF</h1><p>First page content</p>' },
{heading: '<p color="red">Second page content</p>' },
{heading: '<table border="1"><tr bgcolor="pink"><td>Pink rocks!</td><td align="center">Center</td></tr><tr><td colspan="2" align="center">Second row</td></tr></table>' },
{heading: '<img src="' + getAttachmentBase64(att) + '"/>'}
];
gs.debug('Create the PDF!');
// createPDF(html, pages)
// if you don't want to use pages just supply the html argument
formAPI.createPDF('', pages);
function getAttachmentBase64(attachmentGR) {
var base64ImageStr = GlideStringUtil.base64Encode(new GlideSysAttachment().getBytes(attachmentGR));
return "data:image/png;base64," + base64ImageStr + "";
};
})();
View original source
https://www.servicenow.com/community/developer-articles/generating-a-pdf-from-html/ta-p/2306347