How to Rotate Pages in a PDF Document using Node.js

Cloudmersive
3 min readSep 25, 2023

--

Wouldn’t it be great if our web applications could quickly rectify common errors with PDF documents?

It’s especially common to find large, hastily assembled PDF documents with all or several pages rotated at the wrong angles, making the contents difficult to review.

Using the below code, we can take advantage of two APIs designed to rotate all the pages in a PDF document OR only a specific subset of pages in a PDF document by a factor of 90 degrees. We can structure our API calls using ready-to-run node.js code examples, and we can leverage simple request parameters to perform our desired action with uploaded PDF form data. Both APIs perform their request in-memory and release their data cache upon completing their task.

To structure a request for either API, let’s start by installing the SDK. We can either run the below command:

npm install cloudmersive-convert-api-client --save

Or we can add the following snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

We’ll need a free-tier Cloudmersive API key to authorize our request, and we can get one by registering a free account on the Cloudmersive website (this allows a limit of 800 API calls per month with no additional commitment).

We can then use the below code to make an API call that rotates ALL the pages in our document by a specified factor of 90 degrees:

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.EditPdfApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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 callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfRotateAllPages(inputFile, rotationAngle, callback);

Or we can use the below code to make an API call that rotates a specific subset of pages (defined in our request parameters) by a factor of 90 degrees:

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.EditPdfApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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);

We’ll receive our modified document contents in our API response, and we can then create a new file or update our existing file — whichever we chose. Easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet