Import JSpdf in Servicenow

Aman Trivedi1
Tera Contributor

Hi All,

Can Anyone tell me How to import Jspdf in servicenow.

 

Requirement: I have a requirement to download a UI Page using jsPdf, Below is the UI Page Code which download the UI Page in pdf format but by using CDN, our requirement is not to use the CDN("https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js")

<?xml version="1.0" encoding="utf-8"?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
  <html>
    <head>
      <title>My ServiceNow Page</title>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
      <style>
        /* Style the divs */
        div {
          border: 1px solid black;
          margin: 10px;
          padding: 10px;
          background-color: #f2f2f2;
          border-radius: 5px;
        }
        /* Style the button */
        button {
          margin: 10px;
          padding: 10px;
          background-color: black;
          color: white;
          border: none;
          border-radius: 5px;
          cursor: pointer;
        }
        /* Style the button when hovered */
        button:hover {
          background-color: #333;
        }
      </style>
    </head>
    <body>
      <div id="div1">
        <h2>Div 1</h2>
        <p>This is the first div element.</p>
      </div>
      <div id="div2">
        <h2>Div 2</h2>
        <p>This is the second div element.</p>
      </div>
      <div id="div3">
        <h2>Div 3</h2>
        <p>This is the third div element.</p>
      </div>
      <script>
        function popupDispList() {
          // Load the jspdf library
          var script = document.createElement('script');
          script.src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js';
          script.addEventListener('load', function() {
            // The jspdf library has finished loading, so we can now use it in our code
            var doc = new jsPDF();
            // Add text to the PDF document
            doc.text(document.getElementById("div1").innerHTML, 10, 10);
            // Save the PDF document
            doc.addPage();
            doc.text(document.getElementById("div2").innerHTML, 10, 10);
            // Save the PDF document
            doc.addPage();
            doc.text(document.getElementById("div3").innerHTML, 10, 10);
            doc.save("Demo_UI_page.pdf");
          });
          document.head.appendChild(script);
        }
      </script>
      <button onclick="popupDispList()">Generate PDF</button>
    </body>
  </html>
</j:jelly>

  Thanks in Advance.

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Aman Trivedi1 

why to download UI page?

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

Hi Ankur Bawiskar,

We have requirement to download that UI Page in pdf format.
Above code successfully download the UI Page but by Using CDN but we don,t want to use the CDN.

@Aman Trivedi1 

then load that in UI script table as external library

1) create UI script

2) give some name and visit that link ->

https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js

3) copy the entire js and paste and save

4) Ensure you mark Global checkbox as true and also active. Global checkbox may not be on form but you can mark it as true from list

example

AnkurBawiskar_0-1689066131554.png

 


If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi, @Ankur Bawiskar
I have import the jspdf in my PDI as per your instructions, Now how to call the Jspdf by using Below code

 

 <script>
        function popupDispList() {
          // Load the jspdf library
          var script = document.createElement('script');
          script.src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js';
          script.addEventListener('load', function() {
            // The jspdf library has finished loading, so we can now use it in our code
            var doc = new jsPDF();
            // Add text to the PDF document
            doc.text(document.getElementById("div1").innerHTML, 10, 10);
            // Save the PDF document
            doc.addPage();
            doc.text(document.getElementById("div2").innerHTML, 10, 10);
            // Save the PDF document
            doc.addPage();
            doc.text(document.getElementById("div3").innerHTML, 10, 10);
            doc.save("Demo_UI_page.pdf");
          });
          document.head.appendChild(script);
        }
      </script>