How to Crop PDF with cURL
Why Crop PDF Files with cURL?
The pdfRest Set Page Boxes API Tool allows you to define or modify the page boxes of a PDF document, including the CropBox, which controls the visible content area. This tutorial will guide you through the process of sending an API call to set the CropBox using cURL to crop PDF files from the command line.
Consider a scenario where you need to quickly process a PDF and remove unwanted margins directly from your terminal. By using the Set Page Boxes API with cURL to adjust the CropBox, you can efficiently crop the PDF without the need for dedicated desktop software.
Crop PDF with cURL Code Example
# Define the CropBox settings as a JSON string CROP_BOXES='{"boxes":[{"box":"crop","pages":[{"range":"1","left":50,"top":50,"bottom":50,"right":50}]}]}' # Use curl to send a POST request to the pdfRest API endpoint curl -X POST "https://api.pdfrest.com/pdf-with-page-boxes-set" \ -H "Accept: application/json" \ -H "Content-Type: multipart/form-data" \ -H "Api-Key: YOUR_API_KEY" \ -F "file=@/path/to/your/file.pdf" \ -F "boxes=$CROP_BOXES" \ -F "output=cropped_example_out.pdf"
Source: GitHub (Note: This example has been modified to focus on cropping)
Breaking Down the Code for Cropping
The code starts by defining a variable CROP_BOXES
which contains a JSON string. This JSON specifically sets the CropBox for the first page with the desired left, top, bottom, and right margins (in points) to achieve the PDF cropping:
CROP_BOXES='{"boxes":[{"box":"crop","pages":[{"range":"1","left":50,"top":50,"bottom":50,"right":50}]}]}'
The curl
command is used to send a POST request to the pdfRest API endpoint for setting page boxes.
curl -X POST "https://api.pdfrest.com/pdf-with-page-boxes-set"
Headers are set to specify that we expect a JSON response and that we are sending multipart form data. The Api-Key
header is crucial for authentication. Replace YOUR_API_KEY
with your actual pdfRest API key.
-H "Accept: application/json" \ -H "Content-Type: multipart/form-data" \ -H "Api-Key: YOUR_API_KEY"
Form data is included using the -F
option. The file
parameter specifies the path to the PDF file you want to crop. Make sure to replace /path/to/your/file.pdf
with the actual path to your PDF. The boxes
parameter contains the JSON string we defined in the CROP_BOXES
variable. The output
parameter specifies the name of the resulting cropped PDF file.
-F "file=@/path/to/your/file.pdf" \ -F "boxes=$CROP_BOXES" \ -F "output=cropped_example_out.pdf"
Further Exploration for PDF Cropping with cURL
This tutorial demonstrated how to quickly crop PDF files from the command line using cURL and the pdfRest Set Page Boxes API by specifically adjusting the CropBox. You can modify the JSON string in the CROP_BOXES
variable 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. JSON payload examples for setting page boxes, including the CropBox, can be found on GitHub.