How to rotate all pages in a PDF document in Node.JS

Rotating PDF pages in Node.JS can certainly be a bit of a chore to set up. If you don’t have a few hours to kill, I’ve got a better solution in mind: an API. Making this switch will pare our implementation time down to mere minutes. Let me lay it out for you:

We start with client installation:

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

Now we can use an API key to make an instance, from which we add our function call for editPdfRotateAllPages. From there it just needs a file path and angle of rotation, which must be divisible by 90.

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 callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editPdfRotateAllPages(inputFile, rotationAngle, callback);

Shortly after your request, you will have your PDF file back with rotated pages. If you need to rotate a specific subset of pages in your file, we also have the editPdfRotatePageRange function.

Image for post
Image for post

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