Skip to Content
NotebooksPDFPDF tools: a quick tour

PDF tools: a quick tour

Download notebook (.ipynb)

The Indox SDK has 51 PDF tools (compress, rotate, watermark, page numbers, OCR, merge, sign, …). Every tool is one Python method on client.pdf, so there is no endpoint or JSON to write by hand.

Run the cells from top to bottom. You will:

  1. connect to Indox
  2. see the list of tools
  3. look at one tool’s settings
  4. run a tool and download the result
  5. chain tools, each one working on the last one’s output

Each tool run spends a few credits. The last cell shows how many.

1. Connect

Put INDOX_API_KEY (your ak_... key) and INDOX_PUBLIC_BASE_URL (https://indox.org) in the repo .env file, or in Colab Secrets. Results are saved in output/pdf_tools/ next to this notebook.

import json, sys from pathlib import Path REPO = Path.cwd().parents[1] sys.path[:0] = [str(REPO), str(REPO / "languages" / "python")] try: from google.colab import userdata get = userdata.get except ImportError: from tools.env import require_env as get from indox_client import Indox client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_PUBLIC_BASE_URL")) pdf = REPO / "colab" / "pdf" / "samples" / "bitcoin.pdf" OUT = Path.cwd() / "output" / "pdf_tools" OUT.mkdir(parents=True, exist_ok=True) start_credits = client.billing.credits_balance()["credit_amount"] print(f"Connected. Sample file: {pdf.name} ({pdf.stat().st_size:,} bytes)")

2. See every tool

Each row is a category, the Python method name and what the tool does.

tools = client.pdf.list_tools() print(len(tools), "PDF tools\n") for tool in tools: print(f"{tool['category']:<9} client.pdf.{tool['method']:<20} {tool['name']}")

3. Look at one tool

Every setting has a default, so you only pass the ones you want to change.

tool = client.pdf.describe_tool("compress-pdf") print(tool["description"], "\n") for option in tool["options"]: if option["key"] in tool["params"]: print(f"{option['key']} (default {option['default']}): {option['description']}")

4. Run a tool

Every tool works the same way, in three steps: start the job, wait for it, download the file.

job = client.pdf.compress(pdf, compress_level=6) # start client.pdf.wait(job["id"]) # wait small = client.pdf.download(job["id"], OUT / "compressed.pdf") # download print(f"before: {pdf.stat().st_size:,} bytes") print(f"after: {small.stat().st_size:,} bytes -> {small.name}")

5. Chain tools

The same three steps in one small helper. Each tool takes the previous tool’s file: rotate the compressed PDF, then add page numbers, then a watermark.

def run(tool, source, name, **settings): job = tool(source, **settings) client.pdf.wait(job["id"]) return client.pdf.download(job["id"], OUT / name) rotated = run(client.pdf.rotate, small, "rotated.pdf", rotate_angle=90) numbered = run(client.pdf.add_page_numbers, rotated, "numbered.pdf", page_numbers_text="Page {n} of {total}") final = run(client.pdf.watermark, numbered, "final.pdf", watermark_text="DRAFT", watermark_opacity=0.3) for path in (rotated, numbered, final): print(f"{path.name:<13} {path.stat().st_size:,} bytes")

6. Done

Open the files in output/pdf_tools/ to see each step, and check your credits.

end_credits = client.billing.credits_balance()["credit_amount"] print(f"credits: {start_credits} before, {end_credits} after") print("files:", sorted(p.name for p in OUT.glob("*.pdf")))