ARTICLE: Create PDF for Record Producer using PDFGenerationAPI
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2023 07:42 AM - edited 08-05-2023 03:39 AM
Below code will generate a PDF and attach to the record itself.
After Business Rule on the Record Producer Table.
(function executeRule(current, previous /*null when async*/ ) {
var hfInfo = new Object();
hfInfo["HeaderImageAttachmentId"] = "3ff276e3877678945443abb356e"; //image sysid from sys_attachment
hfInfo["HeaderImageAlignment"] = "left";
hfInfo["HeaderImageHeight"] = "30",
hfInfo["FooterImageAttachmentId"] = "121gtfgas6ttfgdweqqxfs7ugd"; //image sysid from sys_attachment
// hfInfo["FooterImageAlignment"] = "left";
// hfInfo["FooterTextAlignment"]: "left"
hfInfo["FooterImageHeight"] = "20";
hfInfo["FooterText"] = "Company Internal";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "false";
hfInfo["TopOrBottomMargin"] = "30";
hfInfo["LeftOrRightMargin"] = "20";
var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var pdfName = current.u_number + ' PDF v' + current.u_version;
var html ='<style>body {font-family: Arial, Helvetica, sans-serif; font-size: 13px; }</style><body> <center style="font-size: 20px;">PDF for Record Producer</center><hr>';
html += '<table style="width: 100%; border-style: hidden" >';
// Table fields
var flds = ['Document ID', 'Document Name', 'Description', 'Version'];
var values = [current.u_number, current.u_name,current.description, current.u_version];
for (var a = 0; a < flds.length; a++) {
html += '<tr><td style="padding:5px; width: 50%;"><b>' + flds[a] + ': </td><td>' + remUndef(values[a].getDisplayValue()) + '</td></tr>';
}
html += '</table><hr><br>';
// Variables from Variable Editor
html += '<center style="font-size: 18px;">Variables Info</center><br>';
html += '<table style="width: 100%; border-style: hidden;" >';
var var = [''Test Variable1 Name", ''Test Variable2 Name", ''Test Variable3 Name", ];
var val = [current.variables.variable1_name, current.variables.variable2_name, current.variables.variable3_name ];
for (a = 0; a < var.length; a++) {
if (remUndef(val[a].getDisplayValue()) != '') {
html += '<tr><td style="padding:5px; width: 70%;">' + var[a] + '</td><td>' + remUndef(val[a].getDisplayValue()) + '</td></tr>';
}
}
html += '</table><br><br>';
// MRVS
var mrvs = current.variables.mrvs_name;
var parser = JSON.parse(mrvs);
if (parser.length > 0) {
html += '<table border="1" style="border-collapse:collapse; page-break-inside: avoid; page-break-after: always; width: 100%;"><caption><b>MRVS</b></caption><tr><th>MRVS Variable1 Label</th><th>MRVS Variable2 Label</th><th>MRVS Variable3 Label</th></tr>';
for (var i = 0; i < parser.length; i++) {
var g= new GlideRecord('cmdb_ci_service');
if(g.get(parser[i].mrvs_variable1_name)){ // mrvs_variable1_name is cmdb_ci_service reference field
var d= g.getValue('name');
}else{
d='';
}
html += '<tr><td style="width: 30%;">' + d + '</td><td style="width: 23%;">' + remUndef(parser[i].mrvs_variable2_name) + '</td><td style="width: 22%;">' + remUndef(parser[i].mrvs_variable3_name) + '</td></tr>';
}
html += '</table><br>';
}
html += '</body>';
var result = v.convertToPDFWithHeaderFooter(html, "table_name", current.sys_id, pdfName, hfInfo);
function remUndef(parm) {
if (typeof parm == 'undefined') {
return '';
} else {
return parm;
}
}
})(current, previous);
0 REPLIES 0