How to Crop PDF with JavaScript in NodeJS
Why Crop PDF Files with JavaScript?
The pdfRest Set Page Boxes API Tool allows you to modify the page boxes of a PDF, including the CropBox, which defines the visible content area. This tutorial will guide you through the process of sending an API call to Set Page Boxes using JavaScript to crop PDF files.
Imagine you have a PDF document with unnecessary margins that you want to remove to focus on the core content for better display or printing. By using the Set Page Boxes API to programmatically adjust the CropBox, you can automate the process of cropping PDFs directly within your JavaScript applications.
Crop PDF with JavaScript Code Example
/** * This request demonstrates how to crop a PDF by adjusting the CropBox. */ var axios = require("axios"); var FormData = require("form-data"); var fs = require("fs"); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append("file", fs.createReadStream("/path/to/your/file.pdf")); // Replace with the actual path to your PDF file const cropBoxOptions = { boxes: [ { box: "crop", pages: [ { range: "1", left: 50, // Adjust these values to your desired crop (in points) top: 50, bottom: 50, right: 50, }, ], }, ], }; data.append("boxes", JSON.stringify(cropBoxOptions)); data.append("output", "cropped_example_out"); // define configuration options for axios request var config = { method: "post", maxBodyLength: Infinity, // set maximum length of the request body url: "https://api.pdfrest.com/pdf-with-page-boxes-set", headers: { "Api-Key": "YOUR_API_KEY", // Replace with your actual API key ...data.getHeaders(), // set headers for the request }, data: data, // set the data to be sent with the request }; // send request and handle response or error axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Source: GitHub Repository (Note: This example has been modified to focus on cropping)
Breaking Down the Code for Cropping
The code starts by importing the necessary modules for making HTTP requests and handling form data.
var axios = require("axios"); var FormData = require("form-data"); var fs = require("fs");
A new FormData
instance is created to hold the PDF file and the cropping parameters. The PDF file is read using fs.createReadStream()
. Make sure to replace "/path/to/your/file.pdf"
with the actual path to your PDF file.
var data = new FormData(); data.append("file", fs.createReadStream("/path/to/your/file.pdf"));
The cropBoxOptions
object is specifically configured to crop the PDF. The "box"
property is set to "crop"
, and the "pages"
array specifies the pages to be cropped (in this case, the first page with "range": "1"
). The "left"
, "top"
, "bottom"
, and "right"
values define the new boundaries of the visible content area in points. Adjust these values to achieve your desired cropping.
const cropBoxOptions = { boxes: [ { box: "crop", pages: [ { range: "1", left: 50, // Adjust these values to your desired crop (in points) top: 50, bottom: 50, right: 50, }, ], }, ], };
This cropBoxOptions
object is converted to a JSON string and appended to the FormData
.
data.append("boxes", JSON.stringify(cropBoxOptions)); data.append("output", "cropped_example_out");
The config
object sets up the Axios request, including the API endpoint URL and your API key in the headers. Replace "YOUR_API_KEY"
with your actual pdfRest API key.
var config = { method: "post", maxBodyLength: Infinity, url: "https://api.pdfrest.com/pdf-with-page-boxes-set", headers: { "Api-Key": "YOUR_API_KEY", ...data.getHeaders(), }, data: data, };
Finally, the Axios library is used to send the POST request to the pdfRest API to crop the PDF. The response or any errors are then logged to the console.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Further Exploration for PDF Cropping
This tutorial demonstrated how to crop PDF files using JavaScript and the pdfRest Set Page Boxes API by specifically manipulating the CropBox. You can modify the cropBoxOptions
to apply different cropping settings to various pages or page ranges within your PDF. Experiment with different margin values to achieve the precise visual outcome you need.
To explore more about the Set Page Boxes API and its capabilities, including setting other page boxes, visit the API Lab and consult the comprehensive API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.