Alle innlegg
|Også tilgjengelig på:DE

PDF Compression API – Shrink PDF File Size

Reduce PDF file size with the MaraDocs optimization API. Intelligent compression, preserved quality. No re-encoding hassles.

Martin Kurtz
APIPDFCompressionDocument ProcessingDeveloper
PDF Compression API – Shrink PDF File Size

Large PDFs – investigation files, expert reports, scanned documents – cause email limits, slow storage, and sluggish document management. At our law firm, a single expert report could be 30 MB. A PDF compression API that reduces file size while preserving readability would make sharing and archiving much easier.

Why Building a PDF Compression Solution Yourself Takes Weeks

If you try to build this yourself, you'll quickly find that Ghostscript, qpdf, and pikepdf can shrink PDFs – but you must choose compression levels, handle different PDF structures, and avoid quality loss. Image downsampling, font embedding, and stream compression each have trade-offs. Building a reliable PDF compression API with consistent results requires experimentation.

How the MaraDocs PDF Compression API Solves This in Minutes

The MaraDocs API provides pdf.optimize for intelligent compression. Upload a PDF, validate it, then optimize. The result is typically significantly smaller with preserved quality – the API handles image downsampling, stream compression, and font embedding internally. No Ghostscript to install, no parameters to tune, no quality vs. size guesswork. One call and you get an optimized handle ready for email, archiving, or the next processing step.

PDF Compression Workflow: Upload, Validate, Optimize

Upload a PDF, validate it for viruses and format, then call pdf.optimize. The API applies intelligent compression – image downsampling, stream optimization – and returns an optimized handle. You can download it or pass it to composition, email attachment handling, or archiving. Optimization fits naturally into any pipeline as the final step before delivery. Files stay in your workspace; no re-upload between validation and optimization.

Get your API key in under a minute

Register for a free account and get your API key in under a minute. Of course we'll provide you with some developer credits.

Try MaraDocs API now →

Why MaraDocs is Different: Workspaces, Webview, and German Data Privacy

Most document APIs force you to upload, process, download, then re-upload for the next step. With MaraDocs, files stay in your workspace. Chain with OCR, composition, or email processing – the optimized PDF handle flows directly to the next operation. No re-upload, fewer round-trips.

When compression quality needs a manual check – or you want to compare before and after – open app.maradocs.io for manual review. Your users get full manual control when automation hits an edge case.

All processing runs in Germany (Maramia GmbH), encrypted at rest and in transit. Workspaces expire after 7 days. No data leaves the EU. For GDPR-sensitive document workflows, this matters.

TypeScript Code for Shrinking PDF File Size

API reference: data/upload, pdf/validate, pdf/optimize, data/download/pdf

import { MaraDocsClient } from "@maramia/maradocs-sdk-ts";
import { okPdf } from "@maramia/maradocs-sdk-ts/models/pdf";

const client = new MaraDocsClient({ workspaceSecret: workspace_secret });

// Upload, validate, optimize, download
const uploaded = await client.data.upload(pdfFile);
const validated = await client.pdf.validate({
  unvalidated_file_handle: uploaded.unvalidated_file_handle,
});
const pdfHandle = okPdf(validated);

const optimized = await client.pdf.optimize({ pdf_handle: pdfHandle });

const blob = await client.data.downloadPdf({ pdf_handle: optimized.pdf_handle });

Python Code for PDF Compression

API reference: data/upload, pdf/validate, pdf/optimize, data/download/pdf

import requests
import time

API_URL = "https://api.maradocs.io/v1"
headers = {"Authorization": f"Bearer {WORKSPACE_SECRET}"}

def poll(url, job_id):
    while True:
        r = requests.get(f"{url}/{job_id}", headers=headers).json()
        if r["status"] == "complete":
            return r["response"]["response"]
        time.sleep(1)

# 1. Upload, 2. Validate
upload = requests.post(f"{API_URL}/data/upload", headers=headers, files={"file": (...)}).json()
val = requests.post(f"{API_URL}/pdf/validate", headers=headers,
    json={"unvalidated_file_handle": upload["unvalidated_file_handle"]}).json()
pdf_handle = poll(f"{API_URL}/pdf/validate", val["job_id"])["pdf_handle"]

# 3. Optimize
opt = requests.post(f"{API_URL}/pdf/optimize", headers=headers, json={"pdf_handle": pdf_handle}).json()
optimized = poll(f"{API_URL}/pdf/optimize", opt["job_id"])

# 4. Download
pdf_resp = requests.get(f"{API_URL}/data/download/pdf", headers=headers,
    params={"pdf_handle": optimized["pdf_handle"]})
with open("compressed.pdf", "wb") as out:
    out.write(pdf_resp.content)

Summary and Next Steps

A PDF compression API that reduces file size with preserved quality is ready. MaraDocs handles optimization as part of a broader document workflow. See PDF Handling, Email Handling, and Image on Blank Page for related use cases.


Try it: MaraDocs API | TypeScript SDK


Abonner på nyhetsbrevet nå

Hold deg oppdatert og motta de siste nyhetene, artikler og ressurser via e-post.