Python SDK
The official Python SDK for PDFik allows seamless integration with both synchronous and asynchronous Python code.
It includes full type hinting, sync/async clients, and automatic retry management powered by httpx and tenacity.
Installation
Install via pip:
pip install pdfik
Quick Start
Convert Public URL to PDF (Sync)
from pdfik import PdfikClient, PdfOptions, MarginOptions
# Initialize the client
client = PdfikClient(api_key="sk_live_...")
# 1. Submit the URL to be rendered
job = client.url_to_pdf(
"https://example.com",
options=PdfOptions(
format="A4",
landscape=False,
print_background=True,
margin=MarginOptions(top="10mm", bottom="10mm")
)
)
print(f"Job created: {job.job_id}. Waiting for rendering...")
# 2. Poll until the job completes
result = client.wait_for_job(job.job_id)
print(f"Job finished! Pages: {result.pages_count}")
# 3. Download the PDF bytes
pdf_bytes = client.download_pdf(job.job_id)
with open("output.pdf", "wb") as f:
f.write(pdf_bytes)
print("PDF saved to output.pdf")
# Close connection pool
client.close()
Convert Raw HTML to PDF (Async)
import asyncio
from pdfik import AsyncPdfikClient, PdfOptions
async def main():
async with AsyncPdfikClient(api_key="sk_live_...") as client:
job = await client.html_to_pdf(
"<h1>Hello World</h1><p>Sent from PDFik Python SDK</p>",
options=PdfOptions(format="Letter")
)
result = await client.wait_for_job(job.job_id)
print(f"Job finished! Status: {result.status}")
pdf_bytes = await client.download_pdf(job.job_id)
asyncio.run(main())
Get the direct download URL
Get the direct API download URL for the generated PDF (requires the "X-API-Key" header to download):
file_info = client.get_file_url(job.job_id)
print(f"Direct Download URL: {file_info.download_url}")
API Reference
PdfikClient (Sync) & AsyncPdfikClient (Async)
# Sync Initialization
client = PdfikClient(api_key: str, base_url: str = None)
# Async Initialization
async_client = AsyncPdfikClient(api_key: str, base_url: str = None)
Methods
url_to_pdf(url, options, webhook_url, meta): Submits a job to convert a public URL to a PDF. Returns aJobCreatedResponse.html_to_pdf(html, options, webhook_url, meta): Submits a job to convert raw HTML markup to a PDF. Returns aJobCreatedResponse.get_job(job_id): Fetches the status of a specific job. Returns aJobStatusResponse.wait_for_job(job_id, timeout_sec, poll_interval_sec): Helper that polls until the job isdoneorfailed.get_file_url(job_id): Returns the direct download URL for the PDF (requires the "X-API-Key" header).download_pdf(job_id): Downloads and returns the PDF file asbytes.