DEV Community

PDFTrusted
PDFTrusted

Posted on Fully Autonomous

Compress a PDF from a Python script in 4 HTTP calls (no API key)

Written by the PDFTrusted team. PDFTrusted is our product; this post shows its REST API.

You have a folder of scanned PDFs that are too big to e-mail or upload, and you want a script to shrink them. Here is the whole flow against the PDFTrusted API, with no sign-up and no key: the API answers as a guest.

What the guest allowance is

When we checked on 3 December 2026, a guest could run 3 jobs a day, with files up to 25 MB and 50 pages, one job at a time, and only successful jobs count. Do not hard-code those numbers: every answer carries usage.caps and usage.remaining, so your script can read the real limits. For more, a free trial key gives 50 jobs a month.

One job is four calls

  1. POST /v1/tasks creates a task for a tool (here compress).
  2. PUT /v1/tasks/{id}/upload sends the file.
  3. POST /v1/tasks/{id}/process runs it.
  4. GET /v1/tasks/{id}/download gives a download link; fetch that link with a plain GET.

Use one HTTP session for all four calls.

import time, uuid
import requests

API = "https://api.pdftrusted.com"
s = requests.Session()

# 1. create the task
r = s.post(f"{API}/v1/tasks",
           json={"tool": "compress", "options": {"level": "recommended"}},
           headers={"Idempotency-Key": str(uuid.uuid4())}, timeout=60)
r.raise_for_status()
task = r.json()
print("plan:", task["usage"]["plan"], "remaining:", task["usage"]["remaining"])
tid = task["task_id"]

# 2. upload the PDF
with open("report.pdf", "rb") as f:
    s.put(f"{API}/v1/tasks/{tid}/upload",
          files={"file": ("report.pdf", f, "application/pdf")}, timeout=120).raise_for_status()

# 3. process it
t0 = time.time()
r = s.post(f"{API}/v1/tasks/{tid}/process", timeout=300)
r.raise_for_status()
print("status:", r.json()["status"], f"in {time.time() - t0:.1f} s")

# 4. get the download link, then the file (the link is on another host: plain GET, no headers)
link = s.get(f"{API}/v1/tasks/{tid}/download", timeout=60).json()["download_url"]
out = requests.get(link, timeout=120)
out.raise_for_status()
assert out.content[:5] == b"%PDF-", "not a PDF"
open("report.compressed.pdf", "wb").write(out.content)
print("saved", len(out.content), "bytes")
Enter fullscreen mode Exit fullscreen mode

What we got

We ran it on a 3-page test PDF we made ourselves (pages stored as images, like a scan):

Size Pages
Input 1,161,797 bytes (1.16 MB) 3
Output (recommended) 38,539 bytes (38.5 KB) 3

The process call answered in 2.2 seconds. Your numbers will differ with your files: a PDF that is mostly text and already small shrinks much less than one made of large images.

Things worth knowing

  • Levels: level can be low, recommended (the default) or extreme; target_mb asks for a size instead.
  • Long jobs: POST /v1/tasks/{id}/process?async=1 answers at once; then poll GET /v1/tasks/{id}?wait=20 until status is done.
  • Idempotency: send an Idempotency-Key on create, so a retried request does not make a second task.
  • Files are deleted automatically 1 hour after the job finishes; DELETE /v1/tasks/{id} deletes them at once.
  • Check before you trust: the download answered 200, the file is not empty and starts with %PDF-.
  • Other tools: GET /v1/tools lists every tool name; the same four calls work for them.

Full reference and a guide written for coding agents: https://developers.pdftrusted.com/agent-guide.md

Questions or a tool that misbehaves on your file? Ask in the comments; we read them.

Top comments (0)