Import JSpdf in Servicenow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2023 11:41 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 12:25 AM
why to download UI page?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 12:28 AM - edited 07-11-2023 12:41 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 02:02 AM
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
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2023 02:28 AM - edited 07-11-2023 02:28 AM
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>