Python Examples

If you want quick copy-pasteable examples, this page gives a Python-first view of the PDF Toolkit.

Install the SDK

pip install toolkitapi

Basic setup

from toolkitapi import PDF

pdf = PDF(api_key="tk_...")

Quick examples

Get PDF info

with PDF(api_key="tk_...") as pdf:
    info = pdf.info(url="https://toolkitapi.io/report.pdf")
    print(info)

Extract text

with PDF(api_key="tk_...") as pdf:
    text = pdf.extract_text(url="https://toolkitapi.io/report.pdf")
    print(text["pages"][0]["text"])

Merge files

with PDF(api_key="tk_...") as pdf:
    merged = pdf.merge(pdf_urls=[
        "https://toolkitapi.io/a.pdf",
        "https://toolkitapi.io/b.pdf",
    ])
    print(merged["file_size"])

Add a watermark

with PDF(api_key="tk_...") as pdf:
    out = pdf.watermark(
        "DRAFT",
        url="https://toolkitapi.io/report.pdf",
    )
    print(out["watermarked_pages"])

OCR a scan

with PDF(api_key="tk_...") as pdf:
    result = pdf.ocr(url="https://toolkitapi.io/scan.pdf")
    print(result["pages"][0]["text"])

Render a template to PDF

with PDF(api_key="tk_...") as pdf:
    result = pdf.from_template(
        template="<html><body><h1>{{ title }}</h1></body></html>",
        variables={"title": "Hello PDF"},
    )
    print(result["page_count"])

Explore by topic