Compress and Reduce PDF File Size on Linux

Information

PSPDFKit Processor has been deprecated and replaced by Document Engine. All PSPDFKit Processor licenses will work as before and be supported until 15 May 2024 (we will contact you about license migration). To start using Document Engine, refer to the migration guide. With Document Engine, you’ll have access to robust new capabilities (read the blog for more information).

You can compress PDFs using PSPDFKit Processor.

Before you get started, make sure Processor is up and running.

You can download and use either of the following sample documents for the examples in this guide:

You’ll be sending multipart POST requests with instructions to Processor’s /build endpoint. To learn more about multipart requests, refer to our blog post on the topic, A Brief Tour of Multipart Requests.

Check out the API Reference to learn more about the /build endpoint and all the actions you can perform on PDFs with PSPDFKit Processor.

Compressing PDFs

To compress a PDF, use the following example:

curl -X POST http://localhost:5000/build \
  -F document=@/path/to/example-document.pdf \
  -F instructions='{
  "parts": [
    {
      "file": "document"
    }
  ],
  "output": {
    "type": "pdf",
    "optimize": {
      "grayscaleText": true,
      "grayscaleGraphics": true,
      "grayscaleFormFields": true,
      "grayscaleAnnotations": true,
      "disableImages": true,
      "mrcCompression": true,
      "imageOptimizationQuality": 2
    }
  }
}' \
  -o result.pdf
POST /process HTTP/1.1
Content-Type: multipart/form-data; boundary=customboundary

--customboundary
Content-Disposition: form-data; name="document"; filename="example-document.pdf"
Content-Type: application/pdf

<PDF data>
--customboundary
Content-Disposition: form-data; name="instructions"
Content-Type: application/json

{
  "parts": [
    {
      "file": "document"
    }
  ],
  "output": {
    "type": "pdf",
    "optimize": {
      "grayscaleText": true,
      "grayscaleGraphics": true,
      "grayscaleFormFields": true,
      "grayscaleAnnotations": true,
      "disableImages": true,
      "mrcCompression": true,
      "imageOptimizationQuality": 2
    }
  }
}
--customboundary--

Licensing

To compress PDFs with PSPDFKit Processor, the Compression feature needs to be included in your license. Contact Sales to add compression to your license. After the new component is added to your license, update the offline LICENSE_KEY in your PSPDFKit Processor configuration.

Linearization

You can perform both linearization and compression in a single request to /build if both features are enabled in your license:

instructions = {
  ...
  output: {
    type: "pdf",
    optimize: {
      grayscaleText: true,
      grayscaleGraphics: true,
      grayscaleFormFields: true,
      grayscaleAnnotations: true,
      disableImages: true,
      mrcCompression: true,
      imageOptimizationQuality: 2,
      linearize: true,
    }
  }
}

To learn more about linearization with PSPDFKit Processor, refer to the linearization guide.