POST /v2/pdf/compress

A new and improved compression API - our 1st version 2 PDF.co endpoint!

Allows for more granular control over PDF compression with enhanced results.

Attributes

Attributes are case-sensitive and should be inside JSON for POST request. for example: { "url": "https://example.com/file1.pdf" }
AttributeTypeRequiredDefaultDescription
urlstringYes-URL to the source file url attribute
configobjectNo-Configuration options. By default this object is pre-defined to give typical options for optimization.
httpusernamestringNo-HTTP auth user name if required to access source URL.
httppasswordstringNo-HTTP auth password if required to access source URL.
pagesstringNoall pagesSpecify page indices as comma-separated values or ranges to process (e.g. “0, 1, 2-” or “1, 2, 3-7”). The first-page index is 0. Use ”!” before a number for inverted page numbers (e.g. “!0” for the last page). If not specified, the default configuration processes all pages. The input must be in string format.
passwordstringNo-Password for the PDF file.
asyncbooleanNofalseSet async to true for long processes to run in the background, API will then return a jobId which you can use with the Background Job Check endpoint. Also see Webhooks & Callbacks
namestringNo-File name for the generated output, the input must be in string format.
expirationintegerNo60Set the expiration time for the output link in minutes. After this specified duration, any generated output file(s) will be automatically deleted from PDF.co Temporary Files Storage. The maximum duration for link expiration varies based on your current subscription plan. To store permanent input files (e.g. re-usable images, pdf templates, documents) consider using PDF.co Built-In Files Storage.
profilesobjectNo-See Profiles for more information.
    outputDataFormatstringNo-If you require your output as base64 format, set this to base64
    JPEGQualityintegerNo25Controls JPEG compression quality from 1 (worst quality, smallest size) to 100 (best quality, largest size).

config

The config object defines granular settings for the compression process as follows:

AttributeDescriptionRequired
imagesContains key/value pairs to define how images should be compressed.Yes
saveContains key/value pairs to define how saving should be performed.Yes

config.images

Image compression options. Each key defines how to treat certain image types in the document. In this way you can target color, grayscale or monochrome images with different approaches.

KeyTypeDescription
colorobjectThe compression options for downsizing & compressing color images.
grayscaleobjectThe compression options for downsizing & compressing grayscale images.
monochromeobjectThe compression options for downsizing & compressing monochrome images.

Each key above takes the same bundle of key/value pairs as follows:

Downsample & Compression object

config.images.color, config.images.grayscale & config.images.monochrome all utlize this object schema.

KeyTypeDescription
skipboolean. Default isfalse.Boolean to indicate whether to skip downsizing & compression altogether.
downsampleobjectAn object which conforms to thedownsample object.
compressionobjectAn object which conforms to thecompression object.

Downsample object

Here you can choose the target ppi / threshold ppi or skip downsampling if you want.

The threshold ppi is the limit for which to start downsizing from and the downsample ppi is the target ppi to meet.

Example: If the threshold ppi (threshold_ppi) is 400 and downsample ppi (downsample_ppi) is 250, it will reduce the resolution to 250 ppi for any images with resolution equal or higher than 400 ppi.
KeyTypeDescription
skipboolean. Default isfalse.Boolean to indicate whether to skip downsizing.
downsample_ppinumberThe target PPI. Default is150forcolorandgrayscale,300formonochrome.
threshold_ppinumberThe threshold PPI. Default is225forcolorandgrayscale,450formonochrome.

Compression object

Here you can choose different compression options for each image type: color, grayscale or monochrome.

KeyTypeDescription
skipboolean. Default isfalse.Boolean to indicate whether to skip compressing.
compression_formatstringAvailable algorithms arejpeg,jpeg2000,CCITTGroup4,CCITTGroup3,zip.
compression_paramsobjectAn object which contains the quality settings.

Compression parameters object

Depending on what you have set for the compression_format algorithm you have different options.

  • jpeg: can choose quality
  • jpeg2000: can choose quality and layers

JPEG

KeyTypeDescription
qualitynumberThe quality setting for the image,0~100. Default is60.

Example

"compression_format": "jpeg",
"compression_params": {
   "quality": 60
}

JPEG2000

| Key | Type | Description | | --- | --- | --- | | quality_mode | string. Either “rates” or “dB” depending on the units you want to use to specify image quality. Default is “rates”. | String used to define quality mode for JPEG2000. | | quality_layers | number[]. A sequence of numbers, each of which represents either an approximate size reduction (if quality mode is “rates”) or a signal to noise ratio value in decibels. If not specified, defaults to a single layer of full quality. Default is[30]ifquality_modeis “rates”. Default is[38.0,34.0,30.0]ifquality_modeis “dB”. | An array of numbers for the layers. |

Example

"compression_format": "jpeg2000",
"compression_params": {
   "quality_mode": "dB",
   "quality_layers": [38.0, 34.0, 31.0]
}

config.save

Contains one key for use with garbage collection to free up space that has been allocated to objects no longer needed.

KeyTypeDescription
garbagenumberGarbage collection option.

config.save.garbage

ValueDescription
0none
1remove unused (unreferenced) objects.
2in addition to 1, compact the xref table.
3in addition to 2, merge duplicate objects.
4(default)in addition to 3, check stream objects for duplication.

Example

"save": {
       "garbage": 4
   }

The default config object

The default config is set to match the standard configuration for image optimization by Adobe Acrobat Pro and is defined as follows:

{
   "images": {
       "color": {
           "skip": false,
           "downsample": {
               "skip": false,
               "downsample_ppi": 150,
               "threshold_ppi": 225
           },
           "compression": {
               "skip": false,
               "compression_format": "jpeg",
               "compression_params": {
                   "quality": 60
               }
           }
       },
       "grayscale": {
           "skip": false,
           "downsample": {
               "skip": false,
               "downsample_ppi": 150,
               "threshold_ppi": 225
           },
           "compression": {
               "skip": false,
               "compression_format": "jpeg",
               "compression_params": {
                   "quality": 60
               }
           }
       },
       "monochrome": {
           "skip": false,
           "downsample": {
               "skip": false,
               "downsample_ppi": 300,
               "threshold_ppi": 450
           },
           "compression": {
               "skip": false,
               "compression_format": "ccitt_g4",
               "compression_params": {}
           }
       }
   },
   "save": {
       "garbage": 4
   }
}

Query parameters

No query parameters accepted.

Responses

ParameterTypeDescription
pageCountintegerNumber of pages in the PDF document.
errorbooleanIndicates whether an error occurred (false means success)
statusstringStatus code of the request (200, 404, 500, etc.). For more information, see Response Codes.
creditsintegerNumber of credits consumed by the request
remainingCreditsintegerNumber of credits remaining in the account
durationintegerTime taken for the operation in milliseconds
urlstringDirect URL to the final PDF file stored in S3.
namestringName of the output file
outputLinkValidTillstringTimestamp indicating when the output link will expire

Example Payload

To see the request size limits, please refer to the Request Size Limits.
{
  "url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-compress/sample.pdf",
  "async": false,
  "config": {
    "images": {
      "color": {
        "skip": false,
        "downsample": {
          "skip": false,
          "downsample_ppi": 150,
          "threshold_ppi": 225
        },
        "compression": {
          "skip": false,
          "compression_format": "jpeg",
          "compression_params": {
            "quality": 60
          }
        }
      },
      "grayscale": {
        "skip": false,
        "downsample": {
          "skip": false,
          "downsample_ppi": 150,
          "threshold_ppi": 225
        },
        "compression": {
          "skip": false,
          "compression_format": "jpeg",
          "compression_params": {
            "quality": 60
          }
        }
      },
      "monochrome": {
        "skip": false,
        "downsample": {
          "skip": false,
          "downsample_ppi": 300,
          "threshold_ppi": 450
        },
        "compression": {
          "skip": false,
          "compression_format": "ccitt_g4",
          "compression_params": {}
        }
      }
    },
    "save": {
      "garbage": 4
    }
  }
}

Example Response

To see the main response codes, please refer to the Response Codes page.
{
  "pageCount": 2,
  "error": false,
  "status": 200,
  "credits": 70,
  "remainingCredits": 999860,
  "duration": 8768,
  "url": "https://pdf-temp-files-stage.s3.amazonaws.com/Q5LRG3405GT14GNBBP8N58IXIUKF6NJO/compress-test-file.pdf?response-content-type=application%2Foctet-stream&response-content-disposition=attachment%3B%20filename%3D%22compress-test-file.pdf%22&AWSAccessKeyId=ASIASXN3AR4TZVP6G2PQ&Signature=h2Ffqd3OBDzaVmEPxJ%2BmTdbLMsI%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEO%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLXdlc3QtMiJIMEYCIQDgXKbzaSp%2FaJrC3SWlRorreN9x%2BbkJ%2BgEHAaOVV7E8ygIhANXdm4YRa9qLMdI2r3qS%2FJzW4qBMTiQQ5tNyxumuDeBkKv4CCEgQABoMMTg3NzYwMTUyMzU5IgySpYVqM8WXbK4IUxUq2wLnZiRWas5oBdBpcNHBtF9dScpq0bVYoBwJRnw0lsLsCMLhutefsj9CGr9ErvwxrYmmDIrZ4vC%2FWJ3y%2Bu6d5tN1CR%2BvjpAcLzS9S0g35ISj3CpJBf6MIQh1VAi0jdBxqHRtnwyePGTGTdNKjyiWAbhTt291muIFdqNLFwEtajiJp3%2FgbeZV1Lz1D64ZmI%2FF8z8uiwIkuSC7B9yMK47nXavLUE4grcV4FIBi72KLYu3Ox4fW3dvgHQ0fidQbuyx25YLg2UytBrdrGVUJNcFaYPisP76dqJXZABYtGShEnx%2FLUvltnRKXDAnbJWF8JvpL7RNmTy9Os5juXjL2q6pI2NGPQWG9Z49CFbiBoDnQbqedbTIx%2F8klYHfe7X84V2fHVSu%2BvvOcP02nd9%2F8zyN%2FUxxausV5bH4rB5Jf925CYa42T5UQ7549bmXfh9zTfCO2ieDEOPhae8mmZ7VMJjDC%2BOC%2BBjqdASP%2BO3P0TicxozEfw9RDPChcqE2yXP2VuEqJV37063fojqHFE1eVPYDjdpMo2OTskboP6PiRL9obIzgBgA4BhTZmQegcOXNEkB57FjGLnoqrRt092FOuqAa0uws1U8DJz6yjlbgxh5bCB6NLtsTJT02MB%2B7O2Bw3j2ILZefWUAELaI9FSkLzSsyHKtWtj2I90oLYomZUb8NkvW5YXkg%3D&Expires=1742228055",
  "name": "compress-test-file.pdf",
  "outputLinkValidTill": "2025-03-17T16:14:15.296664+00:00"
}

Code Samples

curl --location 'https://api.pdf.co/v2/pdf/compress' \
--header 'Content-Type: application/json' \
--header 'x-api-key: *******************' \
--data '{
"async": false,
"url": "https://pdfco-test-files.s3.us-west-2.amazonaws.com/pdf-compress/sample.pdf"
}'