{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cell-00",
   "metadata": {},
   "source": [
    "# PDF tools: a quick tour\n",
    "\n",
    "The Indox SDK has **51 PDF tools** (compress, rotate, watermark, page numbers, OCR, merge, sign, ...).\n",
    "Every tool is one Python method on `client.pdf`, so there is no endpoint or JSON to write by hand.\n",
    "\n",
    "Run the cells from top to bottom. You will:\n",
    "1. connect to Indox\n",
    "2. see the list of tools\n",
    "3. look at one tool's settings\n",
    "4. run a tool and download the result\n",
    "5. chain tools, each one working on the last one's output\n",
    "\n",
    "Each tool run spends a few credits. The last cell shows how many."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-01",
   "metadata": {},
   "source": [
    "## 1. Connect\n",
    "Put `INDOX_API_KEY` (your `ak_...` key) and `INDOX_PUBLIC_BASE_URL` (`https://indox.org`) in the repo `.env` file,\n",
    "or in Colab **Secrets**. Results are saved in `output/pdf_tools/` next to this notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-02",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:21:54.071171Z",
     "iopub.status.busy": "2026-11-06T20:21:54.071039Z",
     "iopub.status.idle": "2026-11-06T20:21:54.905051Z",
     "shell.execute_reply": "2026-11-06T20:21:54.904639Z"
    }
   },
   "outputs": [],
   "source": [
    "import json, sys\n",
    "from pathlib import Path\n",
    "\n",
    "REPO = Path.cwd().parents[1]\n",
    "sys.path[:0] = [str(REPO), str(REPO / \"languages\" / \"python\")]\n",
    "try:\n",
    "    from google.colab import userdata\n",
    "    get = userdata.get\n",
    "except ImportError:\n",
    "    from tools.env import require_env as get\n",
    "\n",
    "from indox_client import Indox\n",
    "\n",
    "client = Indox(api_key=get(\"INDOX_API_KEY\"), base_url=get(\"INDOX_PUBLIC_BASE_URL\"))\n",
    "pdf = REPO / \"colab\" / \"pdf\" / \"samples\" / \"bitcoin.pdf\"\n",
    "OUT = Path.cwd() / \"output\" / \"pdf_tools\"\n",
    "OUT.mkdir(parents=True, exist_ok=True)\n",
    "start_credits = client.billing.credits_balance()[\"credit_amount\"]\n",
    "print(f\"Connected. Sample file: {pdf.name} ({pdf.stat().st_size:,} bytes)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-03",
   "metadata": {},
   "source": [
    "## 2. See every tool\n",
    "Each row is a category, the Python method name and what the tool does."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-04",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:21:54.906421Z",
     "iopub.status.busy": "2026-11-06T20:21:54.906296Z",
     "iopub.status.idle": "2026-11-06T20:21:54.911843Z",
     "shell.execute_reply": "2026-11-06T20:21:54.911453Z"
    }
   },
   "outputs": [],
   "source": [
    "tools = client.pdf.list_tools()\n",
    "print(len(tools), \"PDF tools\\n\")\n",
    "for tool in tools:\n",
    "    print(f\"{tool['category']:<9} client.pdf.{tool['method']:<20} {tool['name']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-05",
   "metadata": {},
   "source": [
    "## 3. Look at one tool\n",
    "Every setting has a default, so you only pass the ones you want to change."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-06",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:21:54.913039Z",
     "iopub.status.busy": "2026-11-06T20:21:54.912919Z",
     "iopub.status.idle": "2026-11-06T20:21:54.915526Z",
     "shell.execute_reply": "2026-11-06T20:21:54.915138Z"
    }
   },
   "outputs": [],
   "source": [
    "tool = client.pdf.describe_tool(\"compress-pdf\")\n",
    "print(tool[\"description\"], \"\\n\")\n",
    "for option in tool[\"options\"]:\n",
    "    if option[\"key\"] in tool[\"params\"]:\n",
    "        print(f\"{option['key']} (default {option['default']}): {option['description']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-07",
   "metadata": {},
   "source": [
    "## 4. Run a tool\n",
    "Every tool works the same way, in three steps: **start** the job, **wait** for it, **download** the file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-08",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:21:54.916744Z",
     "iopub.status.busy": "2026-11-06T20:21:54.916626Z",
     "iopub.status.idle": "2026-11-06T20:22:00.165839Z",
     "shell.execute_reply": "2026-11-06T20:22:00.165413Z"
    }
   },
   "outputs": [],
   "source": [
    "job = client.pdf.compress(pdf, compress_level=6)   # start\n",
    "client.pdf.wait(job[\"id\"])                           # wait\n",
    "small = client.pdf.download(job[\"id\"], OUT / \"compressed.pdf\")  # download\n",
    "\n",
    "print(f\"before: {pdf.stat().st_size:,} bytes\")\n",
    "print(f\"after:  {small.stat().st_size:,} bytes  ->  {small.name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-09",
   "metadata": {},
   "source": [
    "## 5. Chain tools\n",
    "The same three steps in one small helper. Each tool takes the previous tool's file:\n",
    "rotate the compressed PDF, then add page numbers, then a watermark."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-10",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:22:00.167022Z",
     "iopub.status.busy": "2026-11-06T20:22:00.166898Z",
     "iopub.status.idle": "2026-11-06T20:22:16.332910Z",
     "shell.execute_reply": "2026-11-06T20:22:16.332486Z"
    }
   },
   "outputs": [],
   "source": [
    "def run(tool, source, name, **settings):\n",
    "    job = tool(source, **settings)\n",
    "    client.pdf.wait(job[\"id\"])\n",
    "    return client.pdf.download(job[\"id\"], OUT / name)\n",
    "\n",
    "\n",
    "rotated = run(client.pdf.rotate, small, \"rotated.pdf\", rotate_angle=90)\n",
    "numbered = run(client.pdf.add_page_numbers, rotated, \"numbered.pdf\", page_numbers_text=\"Page {n} of {total}\")\n",
    "final = run(client.pdf.watermark, numbered, \"final.pdf\", watermark_text=\"DRAFT\", watermark_opacity=0.3)\n",
    "\n",
    "for path in (rotated, numbered, final):\n",
    "    print(f\"{path.name:<13} {path.stat().st_size:,} bytes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-11",
   "metadata": {},
   "source": [
    "## 6. Done\n",
    "Open the files in `output/pdf_tools/` to see each step, and check your credits."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cell-12",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-11-06T20:22:16.334080Z",
     "iopub.status.busy": "2026-11-06T20:22:16.333957Z",
     "iopub.status.idle": "2026-11-06T20:22:16.540053Z",
     "shell.execute_reply": "2026-11-06T20:22:16.539677Z"
    }
   },
   "outputs": [],
   "source": [
    "end_credits = client.billing.credits_balance()[\"credit_amount\"]\n",
    "print(f\"credits: {start_credits} before, {end_credits} after\")\n",
    "print(\"files:\", sorted(p.name for p in OUT.glob(\"*.pdf\")))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
