How to add Image in converted PDF from RITM

Meenal Gharat
Giga 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
Tera Sage

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

3 REPLIES 3

J Siva
Tera Sage

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
Giga Guru

Hello siva,

 

Thank you for your help.

It worked.

 

Best Regards,

Meenal Gharat

@Meenal Gharat Glad it helped..