Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to add Image in converted PDF from RITM

Meenal Gharat
Tera Guru

Hello,

 

I am trying to add company's logo on top left corner on the header on the PDF.

I am converting this PDF from RITM on Insert using Business rule.

 

Can someone help to correct my code if anything is missing

 

 
// (function executeRule(current, previous /*null when async*/) {

//Add your code here

(function executeRule(current, previous /*null when async*/) {

gs.info("Entered Business Rule: PDF Generation");

// Initialize PDF generation utility
var pdfUtil = new sn_pdfgeneratorutils.PDFGenerationAPI();

// Define the file name
var fileName = current.number + "_Details";

// Create HTML content for the PDF
var html = "<html><body>";
//var logoURL = "https://test.service-now.com/nav_to.do?uri=db_image.do?sys_id=9ad3faf33b46ee90259d122c95e45a29";
var hdrImagAttSysId = "271adb08fb926a100c67f9774eefdcdb";


    // Header with Logo and Title
// html += "<div style='width: 100%;'>";
// html += "<img src='" + logoURL + "' alt='Company Logo' style='height: 60px; float: left;' />";
// //html +='<img src="'+logoURL +'" alt="logo" height="50" style="background-size:contain background-repeat: no-repeat; padding-bottom:5px; margin-bottom:5px;" />'
// html += "</div><br><br>";

html += "<h2 style='text-align: center; font-family: Arial, sans-serif; color: #333;'>Request Details: " + current.number + "</h2>";
html += "<table border='1' cellpadding='10' cellspacing='0' style='width:100%; border-collapse: collapse; font-family: Arial, sans-serif;'>";
html += "<thead>";
html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Field</th>";
html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Value</th>";
html += "</tr>";
html += "</thead>";
html += "<tbody>";

var variables = current.variables;
for (var key in variables) {
    if (variables.hasOwnProperty(key)) {
        var varLabel = variables[key].getLabel();
        var varValue = variables[key].getDisplayValue();
		//gs.log("MG Submitted Values " +varLabel+" - "+varValue);

        // Exclude empty submitted values
        if ((varValue && varValue.trim() !== "")&& (varLabel && varLabel.trim()!=="")) {
            html += "<tr style='background-color: #fff; text-align: left;'>";
            html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varLabel + "</td>";
            html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varValue + "</td>";
            html += "</tr>";
        }
    }
}

// Add Authorized Signature row at the end
html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
html += "<td style='padding: 10px; border: 1px solid #ddd;'>Authorized Signature</td>";
html += "<td style='padding: 10px; border: 1px solid #ddd; text-align: center;'>  </td>"; // Space for Signature
html += "</tr>";

html += "</tbody>";
html += "</table><br><br>";
html += "</body></html>";

// Set header/footer options
// var hfInfo = {
//     "PageSize": "A4",
//     "GeneratePageNumber": "true",
// 	"HeaderImageAttachmentId": hdrImagAttSysId, // <-- Replace with actual sys_id of the logo attachment
//     "HeaderImageAlignment": "left",
// };
var hfInfo = {
    "HeaderImageAttachmentId": hdrImagAttSysId,
    "HeaderImageAlignment": "left",
    "PageSize": "A4",
    "GeneratePageNumber": "true"
};

var hfi = {
    "PageOrientation": "PORTRAIT",
    //"GeneratePageNumber": true
};

// Convert HTML to PDF
var result = pdfUtil.convertToPDFWithHeaderFooter(html, "sc_req_item", current.sys_id, fileName, hfi, '', hfInfo);

// Log the result
gs.info("PDF Generation Result: " + result);


})(current, previous);
​
1 ACCEPTED SOLUTION

J Siva
Kilo Patron
Kilo Patron

Hi @Meenal Gharat 
You should store your company logo in the sys_attachment table and use that sys_id in the script.
I just modified you script and tried in my PDI. It worked.

(function executeRule(current, previous /*null when async*/ ) {
    gs.info("Entered Business Rule: PDF Generation");

    // Initialize PDF generation utility
    var pdfUtil = new sn_pdfgeneratorutils.PDFGenerationAPI();

    // Define the file name
    var fileName = current.number + "_Details";

    // Create HTML content for the PDF
    var html = "<html><body>";
    var hdrImagAttSysId = "ef38bb4cc3de221091ea5242b4013136"; // STORE THE IMAGE IN SYS_ATTACHMENT TABLE AND USE THAT SYS_ID


    html += "<h2 style='text-align: center; font-family: Arial, sans-serif; color: #333;'>Request Details: " + current.number + "</h2>";
    html += "<table border='1' cellpadding='10' cellspacing='0' style='width:100%; border-collapse: collapse; font-family: Arial, sans-serif;'>";
    html += "<thead>";
    html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
    html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Field</th>";
    html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Value</th>";
    html += "</tr>";
    html += "</thead>";
    html += "<tbody>";

    var variables = current.variables;
    for (var key in variables) {
        if (variables.hasOwnProperty(key)) {
            var varLabel = variables[key].getLabel();
            var varValue = variables[key].getDisplayValue();
            //gs.log("MG Submitted Values " +varLabel+" - "+varValue);

            // Exclude empty submitted values
            if ((varValue && varValue.trim() !== "") && (varLabel && varLabel.trim() !== "")) {
                html += "<tr style='background-color: #fff; text-align: left;'>";
                html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varLabel + "</td>";
                html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varValue + "</td>";
                html += "</tr>";
            }
        }
    }

    // Add Authorized Signature row at the end
    html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
    html += "<td style='padding: 10px; border: 1px solid #ddd;'>Authorized Signature</td>";
    html += "<td style='padding: 10px; border: 1px solid #ddd; text-align: center;'>  </td>"; // Space for Signature
    html += "</tr>";

    html += "</tbody>";
    html += "</table><br><br>";
    html += "</body></html>";


    var hfInfo = new Object();
    hfInfo["HeaderImageAttachmentId"] = hdrImagAttSysId;
    hfInfo["HeaderImageAlignment"] = "left";
    hfInfo["PageSize"] = "A4";
    hfInfo["GeneratePageNumber"] = "true";


    // Convert HTML to PDF
    var result = pdfUtil.convertToPDFWithHeaderFooter(html, "sc_req_item", current.sys_id, fileName, hfInfo);

    // Log the result
    gs.info("PDF Generation Result: " + result);
})(current, previous);

 

Regards,
Siva

View solution in original post

5 REPLIES 5

J Siva
Kilo Patron
Kilo Patron

Hi @Meenal Gharat 
You should store your company logo in the sys_attachment table and use that sys_id in the script.
I just modified you script and tried in my PDI. It worked.

(function executeRule(current, previous /*null when async*/ ) {
    gs.info("Entered Business Rule: PDF Generation");

    // Initialize PDF generation utility
    var pdfUtil = new sn_pdfgeneratorutils.PDFGenerationAPI();

    // Define the file name
    var fileName = current.number + "_Details";

    // Create HTML content for the PDF
    var html = "<html><body>";
    var hdrImagAttSysId = "ef38bb4cc3de221091ea5242b4013136"; // STORE THE IMAGE IN SYS_ATTACHMENT TABLE AND USE THAT SYS_ID


    html += "<h2 style='text-align: center; font-family: Arial, sans-serif; color: #333;'>Request Details: " + current.number + "</h2>";
    html += "<table border='1' cellpadding='10' cellspacing='0' style='width:100%; border-collapse: collapse; font-family: Arial, sans-serif;'>";
    html += "<thead>";
    html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
    html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Field</th>";
    html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Value</th>";
    html += "</tr>";
    html += "</thead>";
    html += "<tbody>";

    var variables = current.variables;
    for (var key in variables) {
        if (variables.hasOwnProperty(key)) {
            var varLabel = variables[key].getLabel();
            var varValue = variables[key].getDisplayValue();
            //gs.log("MG Submitted Values " +varLabel+" - "+varValue);

            // Exclude empty submitted values
            if ((varValue && varValue.trim() !== "") && (varLabel && varLabel.trim() !== "")) {
                html += "<tr style='background-color: #fff; text-align: left;'>";
                html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varLabel + "</td>";
                html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varValue + "</td>";
                html += "</tr>";
            }
        }
    }

    // Add Authorized Signature row at the end
    html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
    html += "<td style='padding: 10px; border: 1px solid #ddd;'>Authorized Signature</td>";
    html += "<td style='padding: 10px; border: 1px solid #ddd; text-align: center;'>  </td>"; // Space for Signature
    html += "</tr>";

    html += "</tbody>";
    html += "</table><br><br>";
    html += "</body></html>";


    var hfInfo = new Object();
    hfInfo["HeaderImageAttachmentId"] = hdrImagAttSysId;
    hfInfo["HeaderImageAlignment"] = "left";
    hfInfo["PageSize"] = "A4";
    hfInfo["GeneratePageNumber"] = "true";


    // Convert HTML to PDF
    var result = pdfUtil.convertToPDFWithHeaderFooter(html, "sc_req_item", current.sys_id, fileName, hfInfo);

    // Log the result
    gs.info("PDF Generation Result: " + result);
})(current, previous);

 

Regards,
Siva

Meenal Gharat
Tera Guru

Hello siva,

 

Thank you for your help.

It worked.

 

Best Regards,

Meenal Gharat

@Meenal Gharat Glad it helped..

Here is my Business rule
var pdfUtil = new sn_pdfgeneratorutils.PDFGenerationAPI();

// Define the file name
var fileName_ritm= current.number + "_Details";

// Create HTML content for the PDF
var html = "<html><body>";
var hdrImagAttSysId = "7d6448a0fb922e100c67f9774eefdc37"; // Header image sys_id

html += "<h2 style='text-align: center; font-family: Arial, sans-serif; color: #333;'>Request Details: " + current.number + "</h2>";
html += "<div style='margin-top: 30px;'>";
html += "<table border='1' cellpadding='10' cellspacing='0' style='width:100%; border-collapse: collapse; font-family: Arial, sans-serif;'>";
html += "<thead>";
html += "<tr style='background-color: #f2f2f2; font-weight: bold; text-align: center;'>";
html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Field</th>";
html += "<th style='padding: 10px; border: 1px solid #ddd; width:50%;'>Value</th>";
html += "</tr>";
html += "</thead>";
html += "<tbody>";

// Add variables
var variables = current.variables;
for (var key in variables) {
    if (variables.hasOwnProperty(key)) {
        var varLabel = variables[key].getLabel();
        var varValue = variables[key].getDisplayValue();

        if ((varValue && varValue.trim() !== "") && (varLabel && varLabel.trim() !== "")) {
            html += "<tr style='background-color: #fff; text-align: left;'>";
            html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varLabel + "</td>";
            html += "<td style='padding: 10px; border: 1px solid #ddd;'>" + varValue + "</td>";
            html += "</tr>";
        }
    }
}

// Retrieve and sort signature images from RITM attachments
var sa = new GlideSysAttachment();
var signatureMap = {
    "authorized": null,
    "finance": null,
    "epm": null
};

var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sc_req_item');
attachmentGR.addQuery('table_sys_id', current.sys_id);
attachmentGR.addQuery('file_name', 'CONTAINS', 'signature'); // Match all signature images
attachmentGR.query();

while (attachmentGR.next()) {
    var fileName = attachmentGR.file_name.toLowerCase();
    var bytes = sa.getBytes(attachmentGR);
    var base64Str = GlideStringUtil.base64Encode(bytes);

    if (fileName.indexOf("authorized") !== -1) {
        signatureMap["authorized"] = base64Str;
    } else if (fileName.indexOf("finance") !== -1) {
        signatureMap["finance"] = base64Str;
    } else if (fileName.indexOf("epm") !== -1) {
        signatureMap["epm"] = base64Str;
    }
}

// Add Signature row with sorted labels and images
html += "<tr style='background-color: #f2f2f2;text-align: left;'>";
html += "<td style='padding: 10px; border: 1px solid #ddd;'>Signatures</td>";
html += "<td style='padding: 10px; border: 1px solid #ddd;'>";

for (var key in signatureMap) {
    if (signatureMap[key]) {
        var label = key.charAt(0).toUpperCase() + key.slice(1) + " Signature";
        html += "<div style='margin-bottom: 10px; text-align: center;'>";
        html += "<div style='font-weight: bold; margin-bottom: 5px;'>" + label + "</div>";
        html += "<img src='data&colon;image/png;base64," + signatureMap[key];
        }
}

html += "</td></tr>";
html += "</tbody>";
html += "</table><br><br>";
html += "</div>";
html += "</body></html>";

// Header/Footer Info
var hfInfo = new Object();
hfInfo["HeaderImageAttachmentId"] = hdrImagAttSysId;
hfInfo["HeaderImageAlignment"] = "left";
hfInfo["PageSize"] = "A4";
hfInfo["GeneratePageNumber"] = "true";

// Convert HTML to PDF
var result = pdfUtil.convertToPDFWithHeaderFooter(html, "sc_req_item", current.sys_id, fileName_ritm, hfInfo);​

 

Best Regards.

Meenal