How to Easily Convert Web Content in Nodejs
In this article, we’ll run through a few web conversion APIs we can use to automate the process of screenshotting URLs, converting URLs to PDF, converting HTML to PDF, and converting HTML to plain text.
The benefit here is ease and convenience — we can simply copy from complementary, ready-to-run Nodejs code examples to structure our API calls, and we can authorize our requests with a free API key (this allows us to make up to 800 API calls per month with zero commitments).
First, we need to install the client SDK. We can run the following NPM command:
npm install cloudmersive-convert-api-client --save
Or we can add the Node client to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Next, we can begin calling functions to make our web content conversions. In each function, we can replace the ‘YOUR API KEY’
placeholder string with our own API key string to handle authorization.
We can use the below code to screenshot our URLs:
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 parameters
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebUrlToScreenshot(input, callback);
And we can structure our URL screenshot request using a request object like the below example. This allows us to ask for extra loading wait time (e.g., for websites with lots of media to load) and specify the exact height and width of our screenshot:
{
"Url": "string",
"ExtraLoadingWait": 0,
"ScreenshotWidth": 0,
"ScreenshotHeight": 0
}
Next, we can use the below code to convert our URLs to PDF.
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.UrlToPdfRequest(); // UrlToPdfRequest | URL to PDF request parameters
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebUrlToPdf(input, callback);
We can structure our request in a similar way to our URL screenshot request. This time, instead of controlling the height and width of our PDF image, we can specify the scale factor and determine if we want to include background graphics from the website in our PDF document:
{
"Url": "string",
"ExtraLoadingWait": 0,
"IncludeBackgroundGraphics": true,
"ScaleFactor": 0
}
Next, we can convert our HTML strings to PDF using the below 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.HtmlToPdfRequest(); // HtmlToPdfRequest | HTML to PDF request parameters
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebHtmlToPdf(input, callback);
We can structure this input request much like we structured our URL to PDF request:
{
"Html": "string",
"ExtraLoadingWait": 0,
"IncludeBackgroundGraphics": true,
"ScaleFactor": 0
}
Finally, we can use the remaining code to convert our HTML strings to plain text.
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.HtmlToTextRequest(); // HtmlToTextRequest | HTML to Text request parameters
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebHtmlToTxt(input, callback);
This API action simply strips away the HTML elements enclosing text in our HTML strings:
{
"Html": "string"
}
And that’s all the code we’ll need! Now we can easily take advantage of several web content conversion solutions from a single API library using ready-to-run Nodejs code examples.