How to Compress PDF with cURL

Learn how to compress a PDF file to reduce file size using pdfRest Compress PDF API Tool with cURL
Share this page

Why Use Compress PDF with cURL?

The pdfRest Compress PDF API Tool is an invaluable resource for reducing the size of PDF files while maintaining their quality. This tool can be particularly useful when dealing with large documents that need to be shared or stored efficiently. This tutorial will guide you through the process of sending an API call to Compress PDF using cURL, a powerful command-line tool used for transferring data with URLs.

Imagine you need to email a large PDF report to your colleagues, but your email service has a file size limit. Or perhaps you need to upload a PDF to a website that restricts the upload size. In such scenarios, using the Compress PDF API can significantly reduce the file size, making it easier to share and upload documents without compromising on the quality of the content.

Compress PDF with cURL Code Example

curl -X POST "https://api.pdfrest.com/compressed-pdf" \
  -H "Accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" \
  -F "file=@/path/to/file" \
  -F "output=example_out" \
  -F "compression_level=medium"

Reference to the code: pdf-rest-api-samples on GitHub.

Breaking Down the Code

This cURL command is used to send a POST request to the pdfRest Compress PDF API endpoint:

-X POST "https://api.pdfrest.com/compressed-pdf"

The -X POST flag specifies the request method, which is POST in this case.

Headers are added to the request to indicate the expected response format and the content type of the request:

-H "Accept: application/json"
-H "Content-Type: multipart/form-data"

The Accept: application/json header tells the server that the client expects a JSON response. The Content-Type: multipart/form-data header is used because the request will include a file upload.

The API key is included as a header for authentication:

-H "Api-Key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Replace xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

The file to be compressed is included in the form data:

-F "file=@/path/to/file"

The -F flag is used to specify each piece of form data. The file=@/path/to/file form field specifies the file path for the PDF to be compressed.

Additional form fields specify the output file name and the desired compression level:

-F "output=example_out"
-F "compression_level=medium"

The output field determines the name of the output file, and compression_level can be set to 'low', 'medium', or 'high', depending on the desired balance between file size and quality.

Beyond the Tutorial

By following this tutorial, you have learned how to compress a PDF using the pdfRest Compress PDF API with cURL. This capability can be a game-changer when it comes to managing and distributing PDF files efficiently.

Feel free to explore and demo all of the pdfRest API Tools in the API Lab. For further information and detailed documentation, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.

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