Quick Start for Python
Python offers several libraries for PDF generation, but they can be complex to set up and configure. Our PDF generation API provides a simpler, more flexible way to generate PDFs from HTML, URLs, and dynamic templates. Here's how to start in Python.
1. Get Your API Key
- After registering, log in to the Admin Dashboard and navigate to the API Keys section to retrieve your API key.
- Ensure you keep this key secure, as it is essential for authorizing your requests to the PDFBolt API.
2. Make Your First Request
Use these examples to make requests to the PDFBolt API and generate PDFs quickly.
Choose your preferred endpoint and source combination.
➡️ Endpoints:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage into a PDF:
import requests
import json
url = "https://api.pdfbolt.com/v1/direct"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"url": "https://example.com",
"format": "A4",
"printBackground": true
}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert HTML content directly into a PDF:
import requests
import json
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_json = f'''{{
"html": "{base64_html}",
"margin": {{
"top": "30px",
"left": "30px"
}}
}}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Use reusable templates for dynamic PDF generation:
import requests
import json
url = "https://api.pdfbolt.com/v1/direct"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"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"}
]
}
}'''
data = json.loads(data_json)
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 as e:
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 in your API calls.
The Sync endpoint returns a JSON response with a download URL for the PDF.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage and get download URL:
import requests
import json
url = "https://api.pdfbolt.com/v1/sync"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"url": "https://example.com",
"format": "A4",
"printBackground": true
}'''
data = json.loads(data_json)
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 as e:
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 download URL:
import requests
import json
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_json = f'''{{
"html": "{base64_html}",
"margin": {{
"top": "30px",
"left": "30px"
}}
}}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Use templates and get download URL:
import requests
import json
url = "https://api.pdfbolt.com/v1/sync"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"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"}
]
}
}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
The Async endpoint processes requests asynchronously and sends results via webhook.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage with webhook notification:
import requests
import json
url = "https://api.pdfbolt.com/v1/async"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"url": "https://example.com",
"format": "A4",
"printBackground": true,
"webhook": "https://your-app.com/webhook"
}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Convert HTML with webhook notification:
import requests
import json
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_json = f'''{{
"html": "{base64_html}",
"margin": {{
"top": "30px",
"left": "30px"
}},
"webhook": "https://your-app.com/webhook"
}}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
Use templates with webhook notification:
import requests
import json
url = "https://api.pdfbolt.com/v1/async"
headers = {
"API-KEY": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
"Content-Type": "application/json"
}
data_json = '''{
"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"
}'''
data = json.loads(data_json)
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 as e:
print(f"HTTP {response.status_code}")
print(f"Error Message: {response.text}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
3. What's next?
Continue your journey with PDFBolt by exploring its powerful features and benefits. Start by checking the available API Endpoints and dive into the Conversion Parameters to customize your integration for your unique needs.