Quick Start for Python
Integrate PDFBolt's REST API in Python to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).
1. Get Your API Key
Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.
2. Make Your First Request
Any HTTP client works – adjust the request structure to match your library.
Examples use the requests library. Install it with:
pip install requests
Choose your endpoint:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
Choose your source:
- URL
- HTML
- Template
Convert a webpage to PDF:
import requests
url = "https://api.pdfbolt.com/v1/direct"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"url": "https://example.com",
"format": "A4",
"printBackground": True
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
with open('webpage.pdf', 'wb') as f:
f.write(response.content)
print("PDF generated successfully")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert HTML to PDF (HTML must be base64 encoded):
import requests
import base64
html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64_html = base64.b64encode(html_content.encode()).decode()
url = "https://api.pdfbolt.com/v1/direct"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"html": base64_html,
"format": "A4",
"printBackground": True,
"margin": {
"top": "30px",
"left": "30px"
}
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
with open('document.pdf', 'wb') as f:
f.write(response.content)
print("PDF generated successfully")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert a template with data to PDF:
import requests
url = "https://api.pdfbolt.com/v1/direct"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{"description": "Web Development", "unit_price": "$200.00"},
{"description": "Design Services", "unit_price": "$99.99"}
]
}
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
with open('invoice.pdf', 'wb') as f:
f.write(response.content)
print("PDF generated successfully")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Sync endpoint returns a JSON response with a download URL for the PDF (valid for 24 hours).
Choose your source:
- URL
- HTML
- Template
Convert a webpage and get a download URL:
import requests
url = "https://api.pdfbolt.com/v1/sync"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"url": "https://example.com",
"format": "A4",
"printBackground": True
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"PDF URL: {result['documentUrl']}")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert HTML and get a download URL (HTML must be base64 encoded):
import requests
import base64
html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64_html = base64.b64encode(html_content.encode()).decode()
url = "https://api.pdfbolt.com/v1/sync"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"html": base64_html,
"format": "A4",
"printBackground": True,
"margin": {
"top": "30px",
"left": "30px"
}
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"PDF URL: {result['documentUrl']}")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert a template with data and get a download URL:
import requests
url = "https://api.pdfbolt.com/v1/sync"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{"description": "Web Development", "unit_price": "$200.00"},
{"description": "Design Services", "unit_price": "$99.99"}
]
}
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"PDF URL: {result['documentUrl']}")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
The Async endpoint returns a requestId immediately and delivers the final result via webhook callback.
Choose your source:
- URL
- HTML
- Template
Convert a webpage and receive a webhook callback:
import requests
url = "https://api.pdfbolt.com/v1/async"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"url": "https://example.com",
"format": "A4",
"printBackground": True,
"webhook": "https://your-app.com/webhook"
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"Request ID: {result['requestId']}")
print("PDF will be sent to webhook when ready")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert HTML and receive a webhook callback (HTML must be base64 encoded):
import requests
import base64
html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>"
base64_html = base64.b64encode(html_content.encode()).decode()
url = "https://api.pdfbolt.com/v1/async"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"html": base64_html,
"format": "A4",
"printBackground": True,
"margin": {
"top": "30px",
"left": "30px"
},
"webhook": "https://your-app.com/webhook"
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"Request ID: {result['requestId']}")
print("PDF will be sent to webhook when ready")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert a template with data and receive a webhook callback:
import requests
url = "https://api.pdfbolt.com/v1/async"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data = {
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{"description": "Web Development", "unit_price": "$200.00"},
{"description": "Design Services", "unit_price": "$99.99"}
]
},
"webhook": "https://your-app.com/webhook"
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
result = response.json()
print(f"Request ID: {result['requestId']}")
print("PDF will be sent to webhook when ready")
except requests.exceptions.HTTPError:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Create your first template in the app, then use its ID with JSON data in your API calls. Learn more about Templates
Next Steps
📄️ API Endpoints
📄️ Conversion Parameters
📄️ Error Handling
📄️ Templates
- How to Generate Invoice PDFs with an API – invoice automation with a Python example.
- Print-Ready PDF Generation: PDF/X-1a and PDF/X-4 via API – generate print-ready PDFs, includes Python example.