How to Resize PDF Pages with cURL

Learn how to resize PDF pages using cURL with pdfRest Set Page Boxes API.
Share this page

Why Resize PDF Pages with cURL?

The pdfRest Set Page Boxes API Tool allows you to modify the page boxes of a PDF document, specifically the MediaBox, to control the physical dimensions of the pages. This tutorial will guide you through the process of sending an API call to the Set Page Boxes API Tool endpoint using cURL to resize all pages of a PDF document.

Imagine you have a PDF document where all pages need to conform to a specific standard dimension for consistent display or processing. By using the Set Page Boxes API Tool and the "all" range, you can efficiently resize all PDF pages directly from your command line or scripts.

Resize PDF Pages with cURL Code Example

# Define the MediaBox settings as a JSON string
RESIZE_BOXES='{"boxes":[{"box":"media","pages":[{"range":"all","left":$(( (612 - 595) / 2 )),"top":$(( (792 - 842) / 2 )),"bottom":$(( (792 - 842) / 2 )),"right":$(( (612 - 595) / 2 ))}]}]}'

# 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=$RESIZE_BOXES" \
  -F "output=resized_all_to_a4_out.pdf"

Source: Based on the pdfRest API Documentation

Breaking Down the cURL Command for Resizing All Pages

This command uses curl to send a POST request to the pdfRest API endpoint.

curl -X POST "https://api.pdfrest.com/pdf-with-page-boxes-set"

This specifies the POST method and the API endpoint URL for setting page boxes.

-H "Accept: application/json" \
-H "Content-Type: multipart/form-data" \
-H "Api-Key: YOUR_API_KEY"

These lines set the necessary headers. Remember to replace YOUR_API_KEY with your actual pdfRest API key. The Content-Type is set to multipart/form-data as we are sending a file along with JSON data.

-F "file=@/path/to/your/file.pdf"

This line specifies the PDF file to be processed. Replace /path/to/your/file.pdf with the actual path to your PDF file. The @ symbol tells cURL to upload the file.

# Define the MediaBox settings as a JSON string
RESIZE_BOXES='{"boxes":[{"box":"media","pages":[{"range":"all","left":$(( (612 - 595) / 2 )),"top":$(( (792 - 842) / 2 )),"bottom":$(( (792 - 842) / 2 )),"right":$(( (612 - 595) / 2 ))}]}]}'
-F "boxes=$RESIZE_BOXES"

This section first defines the JSON payload as a shell variable RESIZE_BOXES. Let's break down the JSON:

  • "boxes": The top-level array containing box definitions.
  • "box": "media": Specifies that we are adjusting the MediaBox.
  • "pages": An array of page specifications.
  • "range": "all": Instructs the API to apply the changes to all pages.
  • "left": $(( (612 - 595) / 2 )): Calculates and sets the positive left margin to shrink the width from Letter (612 points) to A4 (595 points).
  • "top": $(( (792 - 842) / 2 )): Calculates and sets the negative top margin to expand the height from Letter (792 points) to A4 (842 points).
  • "bottom": $(( (792 - 842) / 2 )): Calculates and sets the negative bottom margin to expand the height.
  • "right": $(( (612 - 595) / 2 )): Calculates and sets the positive right margin to shrink the width.

Note the use of $((...)) for inline arithmetic calculation in bash.

-F "output=resized_all_to_a4_out.pdf"

This line specifies the desired name for the output PDF file.

Next Steps for Resizing All PDF Pages

This tutorial demonstrated how to use cURL and the pdfRest Set Page Boxes API Tool to resize all pages of a PDF from US Letter to A4 by setting the MediaBox with the "range": "all" option directly from your command line. You can adapt the margin calculations within the RESIZE_BOXES variable to resize to other dimensions. This method is useful for quick tests and integration into shell scripts.

To learn more about the Set Page Boxes API Tool and its capabilities, including setting other page boxes and using different page ranges, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for detailed documentation.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.