Python PDF Generation SDK
The official cloudlayerio package provides Python PDF generation with sync and async support. Convert HTML to PDF, capture URLs as documents, and render templates — with full type safety (mypy strict) and a single runtime dependency (httpx).
- PyPI Package: cloudlayerio
- Source Code: github.com/cloudlayerio/cloudlayerio-python
Installation
pip install cloudlayerio
Requirements: Python 3.9+
Setup
Get your API key by creating a free account at cloudlayer.io. Your account comes with free API credits for testing.
from cloudlayerio import CloudLayer
client = CloudLayer("YOUR-API-KEY", api_version="v2")
The SDK requires you to choose an API version:
| v1 | v2 | |
|---|---|---|
| Default mode | Synchronous (returns binary) | Asynchronous (returns Job) |
| Sync response | Raw binary (PDF/image bytes) | JSON Job object |
| Binary access | Direct from result.data | Via download_job_result() |
URL to PDF
v2 (recommended)
v2 returns a Job object. Use download_job_result() to get the binary:
with CloudLayer("YOUR-API-KEY", api_version="v2") as client:
result = client.url_to_pdf({"url": "https://example.com"})
completed = client.wait_for_job(result.data.id)
pdf_bytes = client.download_job_result(completed)
with open("output.pdf", "wb") as f:
f.write(pdf_bytes)
v1 (direct binary)
v1 returns the binary directly:
with CloudLayer("YOUR-API-KEY", api_version="v1") as client:
result = client.url_to_pdf({"url": "https://example.com"})
with open("output.pdf", "wb") as f:
f.write(result.data) # bytes
HTML to PDF
import base64
html = base64.b64encode(b"<h1>Hello World</h1>").decode()
result = client.html_to_pdf({"html": html})
Template Rendering
result = client.template_to_pdf({
"template_id": "your-template-id",
"data": {"name": "John", "total": "$100"},
})
Async Client
import asyncio
from cloudlayerio import AsyncCloudLayer
async def main():
async with AsyncCloudLayer("YOUR-API-KEY", api_version="v2") as client:
result = await client.url_to_pdf({"url": "https://example.com"})
completed = await client.wait_for_job(result.data.id)
pdf_bytes = await client.download_job_result(completed)
asyncio.run(main())
Error Handling
from cloudlayerio.errors import (
CloudLayerAuthError,
CloudLayerRateLimitError,
CloudLayerError,
)
try:
result = client.url_to_pdf({"url": "https://example.com"})
except CloudLayerAuthError:
print("Invalid API key")
except CloudLayerRateLimitError as e:
print(f"Rate limited, retry after {e.retry_after}s")
except CloudLayerError as e:
print(f"Error: {e}")
Full Documentation
See the README on GitHub for complete documentation including all conversion methods, data management, storage configuration, and performance notes.