Generate a PNG Screenshot of a URL in Node.JS
Taking a screenshot of an unknown URL is a precautionary step that can protect you from possible cyber-attacks. In this brief tutorial, we will demonstrate how you can use one of our conversion APIs in Node.JS to fully render a website and provide a PNG image of the full page; JavaScript, HTML5, CSS, and other advanced features are all supported.
First we need to install our API client by running this command:
npm install cloudmersive-convert-api-client --save
Or, by adding this snippet to the package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Once the installation is complete, we are ready to call the function with the following example code:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';var apiInstance = new CloudmersiveConvertApiClient.ConvertWebApi();var input = new CloudmersiveConvertApiClient.ScreenshotRequest(); // ScreenshotRequest | Screenshot request parametersvar callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebUrlToScreenshot(input, callback);
If you need to retrieve an API key for the operation, you can do so by registering for a free account on the Cloudmersive website; this provides 800 monthly calls across our entire library of APIs.