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

ben_hollifield
Tera Guru

Hi @harshaa0567  - there are a number of ways to accomplish this using the PDF Generation API in ServiceNow. However, if you need a turn-key solution with more flexibility, Yansa Labs offers a certified ServiceNow Store app that makes it easy to create a custom, well-formatted PDF accessible via click-of-a-button on the form. If it's useful, it's free to trial at the link below. Feel free to reach out to me directly if we can help!

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

Can you help me on how to make the button to work on workspace. I'm working on custom application , custom workspace

 

@harshaa0567 

in your original question you didn't mention about workspace

But if you still require then the above code will work fine in workspace as well as it's server side

Just ensure these 2 checkboxes are true and it work fine in workspace as well

AnkurBawiskar_0-1765279903020.png

 

💡 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

Yes I had make one Ui Action and also check the workspace checkbox but on workspace when I click that ui action i.e Download PDF there this page is opening not directly downloading the pdf..??


abcccc.png