How to Change PDF Paper Size using Node.js
The more we can expand the functionality of our PDF-processing web applications, the more we can extend our services to capture a wider audience of users.
The paper size of a PDF usually depends on the intended use-case. For example, legal teams will likely require a different size than creative/marketing teams.
Using the ready-to-run Node.js code examples below, we can take advantage of a free API that allows us to control our PDF paper size by entering any of the 8 possible paper size values (A7 — A0; smallest to largest respectively) in our request. This low-code solution will make it easy to incorporate a useful PDF editing feature in our web app, and we can make up to 800 API calls per month with zero additional commitment with a free-tier API key.
We’ll first need to install the SDK. We can do so by either running the below command:
npm install cloudmersive-convert-api-client --save
Or by adding the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Now we can use the below code examples to call our function. Our uploaded PDF form data will be processed in-memory, and the operation will return the encoding for our new document; the data cache will then be released (deleted).
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 paperSize = "paperSize_example"; // String | The desired paper size for the resized PDF document. Size ranges from A7 (smallest) to A0 (largest).
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfResize(inputFile, paperSize, callback);