Issue generating PDF from UI page

Afifa2
Tera Expert

 

I have created a custom UI Action "Compliance Certificate" on Entity Type. This UI action should pull all the entities, its controls, compliant controls and Non-Compliant controls in a pdf format.

My UI Page :
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<!-- Header -->
  <p>To,</p>
  <p>The Compliance Reviewer,</p>

  <!-- Compliance Report Information -->
  <p>COMPLIANCE REPORT FOR THE PERIOD QTR-1 (January-2024 to Feb-2024)</p>

  <!-- Confirmation Statement -->
  <p>I am in adherence to applicable statutory / regulatory obligations owned by me</p>
<p>A summary of the compliance report covering partial and non-compliance controls are given below</p>
<j:set var="jvar_doc_id" value="${RP.getParameterValue('sysparm_sys_id')}"/>

   <g:evaluate var="jvar_now_entity" object="true">
       var eng=new GlideRecord('sn_grc_m2m_profile_profile_type');
       eng.addEncodedQuery('profile_type=${jvar_doc_id}');
	   eng.query();
	   eng;
   </g:evaluate>
   <div align="center">
   <table align="center" style="border:none;margin-left:auto;margin-right:auto;width: 70%;">
       <tbody>
           <tr style="height:0pt;">
               <td style="border:solid#000000 1pt;vertical-align:top;background-color:#4472c4;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#ffffff;background-color:transparent;font-weight:700;">Compliance Verticle</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;background-color:#4472c4;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#ffffff;background-color:transparent;font-weight:700;">Control Number</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;background-color:#4472c4;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#ffffff;background-color:transparent;font-weight:700;">Complaint Control</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;background-color:#4472c4;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#ffffff;background-color:transparent;font-weight:700;">Non-Compliant Control </p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;background-color:#4472c4;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#ffffff;background-color:transparent;font-weight:700;">Compliance Score</p>
               </td>
               
               </tr>


<j:while test="${jvar_now_entity.next()}">
<g:evaluate var="jvar_now_entity_id" jelly="true" object="true">
var id=jelly.jvar_now_entity.sys_id;
var first_gr=new GlideRecord('sn_grc_m2m_profile_profile_type');
first_gr.get(id)
var control_gr=new GlideRecord('sn_grc_profile');
control_gr.get(first_gr.profile);
control_gr
</g:evaluate>

<g:evaluate var="jvar_now_control_no" jelly="true" object="true">
var id=jelly.jvar_now_entity_id.sys_id;
var control_gr=new GlideRecord('sn_compliance_control');
control_gr.addEncodedQuery('profile='+id);
control_gr.query();
control_gr.getRowCount();
</g:evaluate>
<g:evaluate var="jvar_now_control_no_comp" jelly="true" object="true">
var id=jelly.jvar_now_entity_id.sys_id;
var control_gr=new GlideRecord('sn_compliance_control');
control_gr.addEncodedQuery('profile='+id+'^status=compliant');
control_gr.query();
control_gr.getRowCount();
</g:evaluate>
<g:evaluate var="jvar_now_control_no_non_comp" jelly="true" object="true">
var id=jelly.jvar_now_entity_id.sys_id;
var control_gr=new GlideRecord('sn_compliance_control');
control_gr.addEncodedQuery('profile='+id +'^status=non_compliant');
control_gr.query();
control_gr.getRowCount();
</g:evaluate>
           <tr style="height:0pt;">
               <td style="border:solid#000000 1pt;vertical-align:top;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#000000;background-color:transparent;">${jvar_now_entity_id.getValue('name')}</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#000000;background-color:transparent;">${jvar_now_control_no}</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#000000;background-color:transparent;">${jvar_now_control_no_comp}</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#000000;background-color:transparent;">${jvar_now_control_no_non_comp}</p>
               </td>
               <td style="border:solid#000000 1pt;vertical-align:top;padding:5pt;">
                   <p style="font-size:11pt;font-family:Calibri,sans-serif;color:#000000;background-color:transparent;">${jvar_now_entity_id.getValue('compliance_score')}</p>
               </td>
		   </tr>
</j:while>
</tbody>
   </table>
   </div>

   <p>Yours Sincerely,</p>
  <p>Compliance Owner</p>

</j:jelly>
 

 

 

 

My UI Action:

 
 

 

 

function openCustomPage() {
var url ='sn_compliance_compliance_report.do?sysparm_sys_id='+g_form.getUniqueValue();
top.window.open(url);

}

 

I am able to call UI page .Now I want a button such "Download" on my UI page to download the details mentioned in my UI page in PDF format. Please assist .I just have the below pseudo code for  server script of UI page  but not sure how to embed URL or I should write in Client side on UI page. Kindly assist:

 

 

<body>
    <button onclick="downloadFile()">Download</button>
    <script>
        function downloadFile() {
            // Your code to initiate file download
            // Example:
            window.location.href = '????????????????????????';/// how to give url here ??
        }
    </script>
</body>

 

1 REPLY 1

Rajdeep Ganguly
Mega Guru

To download the details mentioned in your UI page in PDF format, you can use the window.location.href to redirect to a URL that generates a PDF. ServiceNow provides a built-in PDF generation service that you can use. Here are the steps:

1. Create a new UI Page that will generate the PDF. This page will have the same content as your current UI Page, but with a different format to generate a PDF. You can use the GlideSysAttachment API to generate a PDF from a UI Page.

2. In your current UI Page, add a button that when clicked, will redirect to the new UI Page you created. The redirection will trigger the PDF generation and download.

Here is a sample code for the button:

html
Download

 

Replace YOUR_UI_PAGE with the name of the new UI Page you created.

Here is a sample code for the new UI Page:

javascript
var gr = new GlideRecord('table_name');
gr.get('sys_id', sys_id);
var sysAttachment = new GlideSysAttachment();
var attachment = sysAttachment.write(gr, 'file_name', 'application/pdf', generatePDFContent());

function generatePDFContent() {
// Your code to generate the PDF content
// You can use the same code as your current UI Page
}


Replace table_name with the name of the table you are working with, sys_id with the sys_id of the record, and file_name with the name you want for the PDF file.

Please note that this is a simplified example and you might need to adjust the code to fit your needs.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER