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¶
REST API examples (cURL / JavaScript)¶
Get PDF info:
curl "https://pdf.toolkitapi.io/v1/pdf/info?url=https://toolkitapi.io/report.pdf" \
-H "X-API-Key: YOUR_KEY"
const params = new URLSearchParams({ url: "https://toolkitapi.io/report.pdf" });
const resp = await fetch(`https://pdf.toolkitapi.io/v1/pdf/info?${params}`, {
headers: { "X-API-Key": "YOUR_KEY" },
});
const data = await resp.json();
console.log(`${data.pages} pages, ${data.file_size_bytes} bytes`);
Merge PDFs:
curl -X POST "https://pdf.toolkitapi.io/v1/pdf/merge" \
-H "X-API-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"pdf_urls": ["https://toolkitapi.io/a.pdf", "https://toolkitapi.io/b.pdf"]}'
Convert PDF to images:
curl -X POST "https://pdf.toolkitapi.io/v1/pdf/to-images" \
-H "X-API-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://toolkitapi.io/report.pdf", "format": "png", "dpi": 150}'
Python SDK Quick Reference¶
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"])