Skip to Content
NotebooksPDFConversion lifecycle

Conversion lifecycle

Download notebook (.ipynb)

Convert a PDF, then manage the result with the Indox SDK: keep policy, save to My Files, retry (only a failed conversion re-runs) and email (SEND_EMAILS).

Config

Add your settings to the repo .env (local) or Colab Secrets — this cell reads them with get(...): INDOX_API_KEY (your ak_… key) and INDOX_BASE_URL (https://indox.org; the SDK adds /api/v1/… itself). A missing key stops here with its name.

import json, logging, re, sys, tempfile 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 logging.basicConfig(format="→ %(message)s", force=True) logging.getLogger("urllib3.connectionpool").setLevel(logging.DEBUG) def _mask(record): record.msg = re.sub(r"/(public|sign)/[^/ ]+", r"/\1/***", record.getMessage()) record.args = () return True logging.getLogger("urllib3.connectionpool").addFilter(_mask) def show(data, limit=2500): print(json.dumps(data, indent=2, default=str)[:limit]) client = Indox(api_key=get("INDOX_API_KEY"), base_url=get("INDOX_BASE_URL")) start_credits = client.billing.credits_balance()["credit_amount"] SAMPLES = REPO / "colab" out = Path(tempfile.mkdtemp()) pdf = SAMPLES / "pdf" / "samples" / "bitcoin.pdf" SEND_EMAILS = False
job = client.pdf.convert(pdf, target_format="pdf") done = client.pdf.wait(job["id"]) show(done) assert done["status"] == "completed"
kept = client.pdf.set_keep_policy(job["id"], "30d") show(kept) assert kept["keep_policy"] == "30d"
saved = client.pdf.save_to_files(job["id"]) show(saved) assert saved["size_bytes"] > 0
from indox_client import APIStatusError try: retried = client.pdf.retry(job["id"]) except APIStatusError as exc: assert exc.status_code == 409, exc retried = exc.response show(retried) assert retried["status"] in ("pending", "completed")
if SEND_EMAILS: files = client.user.list_files(page_size=20) s3_key = next(f["s3_key"] for f in files["results"] if f["id"] == saved["user_file_id"]) emailed = client.pdf.email(s3_key, filename=saved["file_name"]) show(emailed) else: print("skipped: pdf.email sends the file to your verified address (SEND_EMAILS = False)")

Usage

now = client.billing.credits_balance()["credit_amount"] print(f"credits used by this notebook: {start_credits - now} (balance {now})") show(client.billing.usage_by_feature())
removed = client.user.delete_files({"file_ids": [saved["user_file_id"]]}) show(removed) for cid in {job["id"], retried.get("id", job["id"])}: client.conversion.delete(cid)