0
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery("sys_id",current.sys_id);
ritm.query();
ritm.next();
var filename = ritm.number + '.pdf';
  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 ritmSysId = current.sys_id;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();
 var valuesArray = [];
 var csvHeader = [];
 for(var i=0;i<vs.size();i++){
 var variableLabel = vs.get(i).getLabel();
 csvHeader.push(variableLabel.toString());
 var variableValue = vs.get(i).getDisplayValue();
 valuesArray.push(variableValue.toString());
 }
 var csvHeaderRow = csvHeader.toString();
var valueRow =  valuesArray.toString();
 var document = csvHeaderRow + "\n" + valueRow;
var pages = [
 {heading: document }
];
var ritmRec = new GlideRecord('sc_req_item');
ritmRec.get(ritmSysId);
 gs.debug('Create the PDF!');
 // createPDF(html, pages)
// if you don't want to use pages just supply the html argument
formAPI.createPDF('', document);
 function getAttachmentBase64(attachmentGR) {
 var base64ImageStr = GlideStringUtil.base64Encode(new GlideSysAttachment().getBytes(attachmentGR));
 return "data:image/png;base64," + base64ImageStr + "";
};
Generate PDF with variables detail ServiceNow
admin Changed status to publish December 19, 2023