API Reference and Developer Documentation

PDF Compression

Our PDF compression API optimizes and reduces the size of PDF files. The API provides flexible options to tailor compression to your needs while attempting to maintain a balance between file size and quality.

Understanding Lossless and Lossy Modes

Kraken API automatically determines the optimal compression settings based on the content type of the PDF (e.g., image-heavy, text-heavy, or mixed content). Additionally, users can optionally provide custom parameters to further control the compression process.

Lossless compression is not fully applicable for PDF files, as reducing file size without any quality loss, especially for image-heavy content, is currently not feasible. As such, regardless of whether you set lossy to true or false, the API will operate in lossy mode.

Optional Parameters for Custom Compression

levelOptimization level for the PDF. Overrides DPI values. Available options:
  • screen: Optimized for on-screen viewing.
  • ebook: Balanced for E-Books.
  • printer: Higher quality for printing.
  • prepress: Maximum quality for professional printing.
qualityJPEG quality for embedded images. Acceptable values: 1-100. Higher values preserve more detail but increase file size. Default is 65.
dpiResolution for images within the PDF. Default value is calculated based on the PDF type. Adjust this value based on your desired output quality.
downsampleTypeMethod used to downsample images:
  • bicubic: Smooth and high-quality resizing for detailed images.
  • average: (Default) Averages pixel values to reduce size, faster but less smooth than bicubic.
  • subsample: Picks every nth pixel, very fast but may result in significant quality loss.

Example Usage

{
    "auth": {
        "api_key": "your_api_key",
        "api_secret": "your_api_secret"
    },
    "url": "https://example.com/document.pdf",
    "wait": true,
    "lossy": true,
    "level": "ebook",
    "quality": 80,
    "dpi": 150,
    "downsampleType": "bicubic"
}
<?php

require_once("Kraken.php");

$kraken = new Kraken("your_api_key", "your_api_secret");

$params = array(
    "url" => "https://example.com/document.pdf",
    "wait" => true,
    "lossy" => true,
    "level" => "ebook",
    "quality" => 80,
    "dpi" => 150,
    "downsampleType" => "bicubic"
);

$data = $kraken->url($params);

if ($data["success"]) {
    echo "Success. Optimized PDF URL: " . $data["kraked_url"];
} else {
    echo "Fail. Error message: " . $data["message"];
}
var Kraken = require("kraken");

var kraken = new Kraken({
    "api_key": "your_api_key",
    "api_secret": "your_api_secret"
});

var params = {
    url: "https://example.com/document.pdf",
    wait: true,
    lossy: true,
    level: "ebook",
    quality: 80,
    dpi: 150,
    downsampleType: "bicubic"
};

kraken.url(params, function(status) {
    if (status.success) {
        console.log("Success. Optimized PDF URL: %s", status.kraked_url);
    } else {
        console.log("Fail. Error message: %s", status.message);
    }
});
require 'rubygems'
require 'kraken-io'

kraken = Kraken::API.new(
    :api_key => 'your_api_key',
    :api_secret => 'your_api_secret'
)

params = {
    :url => 'https://example.com/document.pdf',
    :wait => true,
    :lossy => true,
    :level => 'ebook',
    :quality => 80,
    :dpi => 150,
    :downsampleType => 'bicubic'
}

data = kraken.url(params)

if data.success
    puts 'Success! Optimized PDF URL: ' + data.kraked_url
else
    puts 'Fail. Error message: ' + data.message
end
from krakenio import Client

api = Client('your_api_key', 'your_api_secret')

data = {
    'url': 'https://example.com/document.pdf',
    'wait': True,
    'lossy': True,
    'level': 'ebook',
    'quality': 80,
    'dpi': 150,
    'downsampleType': 'bicubic'
}

result = api.url('https://example.com/document.pdf', data);

if result.get('success'):
    print(result.get('kraked_url'))
else:
    print(result.get('message'))