How to rotate a range of pages in a PDF Document in Node.JS

Our goal today is simple: we need to use Node.JS to rotate a subset of pages in a PDF file. If you are looking for the difficult, manual method for accomplishing this, you are in the wrong place. Today we are all about the shortcuts.

First up, add this reference to your package.json. This will allow us to use the Cloudmersive API client for document conversion.

"dependencies": {
"cloudmersive-convert-api-client": "^2.1.6"

And now we are going to write up our function call. This example code will give you an idea of how that should be set up:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var rotationAngle = 56; // Number | The angle to rotate the page in degrees, must be a multiple of 90 degrees, e.g. 90, 180, 270, or -90, -180, -270, etc.var pageStart = 56; // Number | Page number (1 based) to start rotating pages from (inclusive).var pageEnd = 56; // Number | Page number (1 based) to stop rotating pages from (inclusive).var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editPdfRotatePageRange(inputFile, rotationAngle, pageStart, pageEnd, callback);

Whew, already done!

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store