This repository was archived by the owner on Aug 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
PDF Generation with Chrome Puppeteer sample
Richard Szolár edited this page May 7, 2020
·
2 revisions
Here is a sample on how to generate a PDF from an html string with puppeteer and chrome through your Servicenow instance.
// require puppeteer, fs and request packages, these are preinstalled in your node vm already if you've used the repo
const puppeteer = require('puppeteer');
const fs = require('fs');
const request = require('request');
// async function for generating the pdf
async function printPDF() {
// let's make a connection to the chrome headless browser, notice the IP, this is the address of the container running the browser instance
const browser = await puppeteer.connect({
browserURL: "http://172.20.128.4:9222",
headless: true,
slowMo: 250
});
// open a new tab
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080
});
// get the number of the incident from our bootstrap script
let incNum = _getData()['result'][0]['number'];
// html contents
const htmlContent = '<html>' +
'<head>' +
'<title>Some nice title</title>' +
'</head>' +
'<body>' +
'<h3>This is a h3 heading for ' + incNum + '</h3>' +
'<img src="https://images.pexels.com/photos/1130980/pexels-photo-1130980.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="picture here" />' +
'</body>' +
'</html>';
// set the page's content to the html string
await page.setContent(htmlContent);
await page.emulateMedia("screen");
// save the pdf locally to a temporary folder, you need to make sure that the node vm has access to the folder
await page.pdf({
path: "./tmp/rendered_from_html.pdf",
format: 'A4'
});
// after render and save, close the page
await page.close();
// disconnect from the browser
await browser.disconnect();
// read the contents of the file into the variable
const pdfFileData = base64_encode("./tmp/rendered_from_html.pdf");
// use the postback function to send back the contents to the log entry table
doPostBack(_getLogID(), {
pdf: pdfFileData
});
}
// function to encode file data to base64 encoded string
function base64_encode(file) {
// read binary data, in sync mode, not ideal, but for the demo purpose it is enough
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
const doPostBack = (logID, data) => {
// use basic auth for api authentication user=rest.default & password=rest.default
const baseUrl = "https://rest.default:rest.default@instancename.service-now.com";
const endpoint = baseUrl + "/api/x_321937_snc_node/postback";
request.post(endpoint, {
json: {
logID: logID,
result: data
}
}, (error, res, body) => {
if (error) {
console.error(error);
return
}
console.log(`statusCode: ${res.statusCode}`);
console.log(body)
})
};
// call the async function
printPDF();
For a full example and explanation, please visit the blog post Blog post
Servicenow x NodeJS @ 2019