Python bindings for the Api2Pdf REST API.
Api2Pdf.com is a document generation and file conversion API for Python applications. It supports HTML to PDF, URL to PDF, HTML to image, URL to image, Office document conversion, markdown conversion, structured PDF extraction, zip generation, barcode and QR code generation, PDF page extraction, PDF password protection, and thumbnails or previews for PDF, office, and email files.
The package keeps a simple Pythonic client shape with service groups like client.Chrome, client.WkHtml, and client.PdfSharp.
Install from PyPI:
pip install api2pdfCreate an account at portal.api2pdf.com to get your API key.
from api2pdf import Api2Pdf
client = Api2Pdf("YOUR-API-KEY")
result = client.Chrome.html_to_pdf("<html><body><h1>Hello, world!</h1></body></html>")
if result.result["Success"]:
print(result.result["FileUrl"])
else:
print(result.result["Error"])The default client base URL is https://v2.api2pdf.com.
To send requests to a custom Api2Pdf domain:
from api2pdf import Api2Pdf
client = Api2Pdf("YOUR-API-KEY", base_url="https://your-custom-domain.api2pdf.com")You can also attach a tag header to all requests:
client = Api2Pdf("YOUR-API-KEY", tag="billing-batch")Most conversion methods return an Api2PdfResponse.
{
"FileUrl": "https://link-to-file-available-for-24-hours",
"MbOut": 0.08830547332763672,
"Cost": 0.00017251586914062501,
"Seconds": 2,
"Success": True,
"Error": None,
"ResponseId": "6e46637a-650d-46d5-af0b-3d7831baccbb",
}Important members:
result: Parsed JSON response when the API returns standard JSON.is_binary:Truewhenoutput_binary=Truereturns file bytes directly.get_file_bytes(): Return bytes from a binary response or download them fromFileUrl.download_file(): Backward-compatible alias forget_file_bytes().save_file(path): Save the generated file to disk.request: Request metadata useful for debugging.
For debugging:
response = client.Chrome.html_to_pdf("<p>Hello, world!</p>")
print(response)Most conversion methods support some or all of these keyword arguments:
file_name: Set the output file name.inline: Control browser inline display behavior.use_custom_storageandstorage: Send output directly to your own storage target.extra_http_headers: Forward custom headers when Api2Pdf fetches a source URL.output_binary: Request raw file bytes instead of the standard JSON payload when the endpoint supports it.use_get=True: Use the simple GET-style URL endpoints for selected URL conversions.
Example custom storage configuration:
request_storage = {
"method": "PUT",
"url": "https://your-presigned-upload-url",
}
result = client.Chrome.html_to_pdf(
"<p>Hello World</p>",
use_custom_storage=True,
storage=request_storage,
)html_pdf = client.Chrome.html_to_pdf(
"<p>Hello World</p>",
DisplayHeaderFooter=True,
HeaderTemplate="<div style='font-size:12px;'>Header</div>",
FooterTemplate="<div style='font-size:12px;'>Footer</div>",
Landscape=True,
PreferCSSPageSize=True,
)
url_pdf = client.Chrome.url_to_pdf(
"https://www.api2pdf.com",
extra_http_headers={"Authorization": "Bearer token-for-the-source-site"},
PuppeteerWaitForMethod="WaitForNavigation",
PuppeteerWaitForValue="Load",
)For the simple GET route:
result = client.Chrome.url_to_pdf("https://www.api2pdf.com", use_get=True)result = client.Chrome.markdown_to_pdf("# Invoice\n\nGenerated from markdown.")html_image = client.Chrome.html_to_image(
"<p>Hello image</p>",
FullPage=True,
ViewPortOptions={"Width": 1440, "Height": 900},
)
url_image = client.Chrome.url_to_image("https://www.api2pdf.com", use_get=True)
markdown_image = client.Chrome.markdown_to_image("# Screenshot\n\nGenerated from markdown.")html_pdf = client.WkHtml.html_to_pdf(
"<p>Hello World</p>",
orientation="landscape",
pageSize="Letter",
)
url_pdf = client.WkHtml.url_to_pdf(
"https://www.api2pdf.com",
enable_toc=True,
toc_options={"disableDottedLines": "true"},
)For the simple GET route:
result = client.WkHtml.url_to_pdf("https://www.api2pdf.com", use_get=True)Use LibreOffice endpoints for file and Office conversions.
Convert a file URL to PDF:
result = client.LibreOffice.any_to_pdf(
"https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
)Generate a thumbnail:
result = client.LibreOffice.thumbnail(
"https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
)Convert HTML or a URL to DOCX or XLSX:
docx = client.LibreOffice.html_to_docx(
html="<html><body><h1>Hello Word</h1></body></html>"
)
xlsx = client.LibreOffice.html_to_xlsx(
url="https://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html"
)Convert a file URL to markdown:
result = client.Markitdown.convert_to_markdown("https://example.com/sample.docx")Extract structured content from a PDF URL:
json_result = client.OpenDataLoader.pdf_to_json("https://example.com/sample.pdf")
markdown_result = client.OpenDataLoader.pdf_to_markdown("https://example.com/sample.pdf")
html_result = client.OpenDataLoader.pdf_to_html("https://example.com/sample.pdf")Merge PDFs:
result = client.PdfSharp.merge(
[
"https://LINK-TO-PDF-1",
"https://LINK-TO-PDF-2",
]
)Set a password:
result = client.PdfSharp.add_password(
"https://LINK-TO-PDF",
user_password="password",
owner_password="owner-password",
)Extract a page range:
result = client.PdfSharp.extract_pages(
"https://LINK-TO-PDF",
start=0,
end=2,
)Create a zip from multiple files:
result = client.Zip.generate_zip(
[
{"url": "https://example.com/report.pdf", "fileName": "docs/report.pdf"},
{"url": "https://example.com/image.png", "fileName": "images/image.png"},
],
output_binary=True,
)
zip_bytes = result.get_file_bytes()Generate a barcode or QR code:
result = client.Zebra.generate_barcode(
"QR_CODE",
"https://www.api2pdf.com",
width=300,
height=300,
show_label=False,
)Delete a generated file:
result = client.Chrome.html_to_pdf("<p>Hello World</p>")
client.Utilities.delete(result.result["ResponseId"])Check API status or remaining balance:
status = client.Utilities.status()
balance = client.Utilities.balance()Top-level shortcuts are also available:
client.delete(result.result["ResponseId"])
print(client.status())
print(client.balance())Save a generated file to disk:
result = client.Chrome.html_to_pdf("<p>Hello World</p>")
result.save_file("path-to-local-file.pdf")Get bytes directly:
result = client.Chrome.html_to_pdf(
"<p>Hello World</p>",
output_binary=True,
)
file_bytes = result.get_file_bytes()Install the project in editable mode with test dependencies:
python3 -m venv .venv
./.venv/bin/pip install -e .[test]Run the test suite:
./.venv/bin/python -m pytest