Export PDF with only few fields

harshaa0567
Tera Contributor

Hi,

 I have a requirement to create a UI button on the form that Should export only a particular section on the form. For example in incident form I want to export only "resolution notes" section as PDF.

 Can anyone please provide me the solution for it.

 

Thanks in advance

1 ACCEPTED SOLUTION

VaishnaviK43271
Tera Contributor
    var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
    var html;
    var gr = new GlideRecord("incident");
    if (gr.get(current.sys_id)) {

        html = "<html><body>";
        html += "<h2>Resolution Notes</h2>";
        html += "<p><b>Incident:</b> " + gr.number + "</p>";
        html += "<p><b>Short Description:</b> " + gr.short_description + "</p>";
        html += "<hr>";
        html += "<p>" + (gr.close_notes || "No resolution notes available.") + "</p>";
        html += "</body></html>";

        var result = pdf.convertToPDF(html, 'incident', current.sys_id, "Incident Pdf");

        if (result.status ==='success' && result.attachment_id) {
 
            var downloadURL = '/sys_attachment.do?sys_id=' + result.attachment_id;
            var gURL = new GlideURL(downloadURL);
            action.setRedirectURL(gURL);
            //action.setReturnURL(url);
            gs.addInfoMessage("PDF generated successfully.");

        } else {
            gs.addErrorMessage("PDF generation failed.");

        }
    }

 

Screenshot 2025-12-08 122040.png

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for.

Thank You

View solution in original post

10 REPLIES 10

Jennifer Metz
Giga Guru

Hello @harshaa0567,

 

You would need to make a scripted UI action, then format the html to include the needed fields. Here is a post with a rough example of the code. Here is also another post on how to work with PDFGenerationAPI.

 

I hope this helps!

 

Jennifer Metz
Sr. ServiceNow Developer | Infosys

Ankur Bawiskar
Tera Patron
Tera Patron

@harshaa0567 

you can check the PDF Generation API for this

Generating Custom PDFs - using the new PDFGenerationAPI

another method

Generate PDF and attach to the record

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

adityahubli
Tera Contributor

Hello @harshaa0567 ,

 

Refer this code snippet,


 (Option) get HTML from the description field of an incident record
var gr = new GlideRecord("incident");
var html;

if (gr.get(current.sys_id)) {
    html = "Description:" + gr.description.toString();
    html += "\nShort Description:" + gr.short_description.toString();
    html += "\nNumber:" + gr.number.toString();
    html += "\nPriority:" + gr.getDisplayValue('priority');
    html += "\n Assignment Group:" + gr.getDisplayValue('assignment_group');
    html += "\n Assige to:" + gr.getDisplayValue('assigned_to');
}

var result = v.convertToPDF(html, "incident", current.sys_id, "myPDF");
gs.info(JSON.stringify(result));
if (result) {
    action.setRedirectURL(current);
}
 
 
or  you can reffer fololowing code with some html enhance,
 
// Create PDF API object
var v = new sn_pdfgeneratorutils.PDFGenerationAPI();
var html = '';

var gr = new GlideRecord('incident');
if (gr.get(current.sys_id)) {

    // Build HTML with header + styling
    html += '<html>';
    html += '<head>';
    html += '<meta charset="utf-8" />';
    html += '<style>';
    html += 'body { font-family: Arial, sans-serif; font-size: 12px; color: #333; }';
    html += '.header { text-align: center; font-size: 20px; font-weight: bold; ';
    html += '         padding: 10px 0; border-bottom: 2px solid #444; margin-bottom: 20px; }';
    html += '.sub-header { text-align: center; font-size: 11px; color: #777; margin-top: -10px; margin-bottom: 20px; }';
    html += '.meta-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }';
    html += '.meta-table th, .meta-table td { padding: 6px 8px; border: 1px solid #ccc; text-align: left; }';
    html += '.meta-table th { background: #f2f2f2; font-weight: bold; }';
    html += '.section-title { font-size: 14px; font-weight: bold; margin: 10px 0 5px 0; }';
    html += '.section-box { border: 1px solid #ccc; padding: 8px; min-height: 40px; }';
    html += '</style>';
    html += '</head>';
    html += '<body>';

    // Header
    html += '<div class="header">Incident Summary Report</div>';
    html += '<div class="sub-header">Generated from ServiceNow</div>';

    // Basic details as table
    html += '<table class="meta-table">';
    html += '<tr><th>Number</th><td>' + gr.number + '</td></tr>';
    html += '<tr><th>Short Description</th><td>' + (gr.short_description || '') + '</td></tr>';
    html += '<tr><th>Priority</th><td>' + gr.getDisplayValue('priority') + '</td></tr>';
    html += '<tr><th>Assignment Group</th><td>' + gr.getDisplayValue('assignment_group') + '</td></tr>';
    html += '<tr><th>Assigned To</th><td>' + gr.getDisplayValue('assigned_to') + '</td></tr>';
    html += '<tr><th>Opened By</th><td>' + gr.getDisplayValue('caller_id') + '</td></tr>';
    html += '<tr><th>Opened On</th><td>' + gr.getDisplayValue('opened_at') + '</td></tr>';
    html += '</table>';

    // Description section
    var desc = gr.description ? gr.description.toString() : '';
    // Replace newlines with <br> for PDF
    desc = desc.replace(/\n/g, '<br/>');

    html += '<div class="section-title">Description</div>';
    html += '<div class="section-box">' + desc + '</div>';

    html += '</body></html>';
}

// Generate PDF and attach to incident
var result = v.convertToPDF(html, 'incident', current.sys_id, 'Incident PDF - ' + gr.number);

if (result.status === 'success' && result.attachment_id) {
    var downloadURL = gs.getProperty('glide.servlet.uri') + 'sys_attachment.do?sys_id=' + result.attachment_id;
    gs.addInfoMessage('Incident PDF generated successfully. Attachment ID: ' + result.attachment_id);
    // If UI messages allow HTML, you can use:
    // gs.addInfoMessage('Incident PDF generated: <a href="' + downloadURL + '" target="_blank">Download</a>');
} else {
    gs.addErrorMessage('PDF generation failed or no attachment ID returned.');
}
 
 
If  this helps you then mark it as helpfull and accept as solution for future community queries.
Regards,
Aditya

Hello @harshaa0567 ,

If above solution helps you then mark it as helpful and accept as solution.

Regards,

Aditya