Documents¶
2 endpoints for converting between PDF, DOCX, EPUB, HTML, DOC, RTF, ODT, and TXT formats.
| Method | Endpoint | Purpose |
|---|---|---|
POST |
/v1/convert/document |
Convert between document formats |
GET |
/v1/convert/document |
Stream a converted document from a URL source |
Python SDK Examples¶
DOCX to PDF¶
Pass the file as base64-encoded bytes.
import base64
from pathlib import Path
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
docx_bytes = Path("report.docx").read_bytes()
result = cv.document(
from_format="docx",
to_format="pdf",
file=base64.b64encode(docx_bytes).decode(),
)
# result["result"] is the base64-encoded PDF
pdf_bytes = base64.b64decode(result["result"])
Path("report.pdf").write_bytes(pdf_bytes)
PDF to DOCX from a URL¶
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
result = cv.document(
from_format="pdf",
to_format="docx",
url="https://toolkitapi.io/documents/invoice.pdf",
)
import base64
from pathlib import Path
Path("invoice.docx").write_bytes(base64.b64decode(result["result"]))
HTML to PDF¶
Pass inline HTML directly — no base64 encoding needed.
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
html_content = """
<html>
<body>
<h1>Invoice #1042</h1>
<p>Due: 2025-08-01</p>
<table>
<tr><th>Item</th><th>Amount</th></tr>
<tr><td>Consulting</td><td>$1,200</td></tr>
</table>
</body>
</html>
"""
result = cv.document(
from_format="html",
to_format="pdf",
html=html_content,
page_size="A4",
)
import base64
from pathlib import Path
Path("invoice.pdf").write_bytes(base64.b64decode(result["result"]))
PDF to images (specific pages)¶
Convert selected pages of a PDF to PNG images. Use pages to specify a range or comma-separated list.
import base64
from pathlib import Path
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
pdf_bytes = Path("presentation.pdf").read_bytes()
result = cv.document(
from_format="pdf",
to_format="png",
file=base64.b64encode(pdf_bytes).decode(),
pages="1-3",
dpi=150,
)
# Each page is a separate base64 image in result["pages"]
for i, page in enumerate(result.get("pages", [])):
Path(f"slide_{i + 1}.png").write_bytes(base64.b64decode(page["image"]))
Convert from URL and save file directly¶
Use document_file() to receive raw bytes, bypassing the base64 JSON wrapper.
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
path = cv.document_file(
url="https://toolkitapi.io/docs/manual.docx",
from_format="docx",
to_format="pdf",
page_size="Letter",
output_path="manual.pdf",
)
print(f"Saved to {path}")
EPUB to PDF with metadata¶
import base64
from pathlib import Path
from toolkitapi import Convert
cv = Convert(api_key="tk_...")
epub_bytes = Path("book.epub").read_bytes()
result = cv.document(
from_format="epub",
to_format="pdf",
file=base64.b64encode(epub_bytes).decode(),
page_size="A5",
title="My Book",
author="Jane Doe",
)
Path("book.pdf").write_bytes(base64.b64decode(result["result"]))
Request Parameters¶
| Parameter | Type | Description |
|---|---|---|
from_format |
string | Source format: pdf, docx, doc, epub, html, rtf, odt, txt |
to_format |
string | Target format: same set, plus png / jpeg for PDF→images |
file |
string | Base64-encoded source file (alternative to url or html) |
url |
string | Public URL to fetch source file from |
html |
string | Inline HTML string — only valid when from_format="html" |
pages |
string | Page range for PDF→images, e.g. "1-3" or "1,3,5" |
dpi |
integer | Image resolution for PDF→images (default 150) |
page_size |
string | Output page size: A3, A4, A5, Letter, Legal |
title |
string | Document title metadata |
author |
string | Document author metadata |
Response Fields¶
| Field | Type | Description |
|---|---|---|
result |
string | Base64-encoded converted file (single document output) |
pages |
array | Array of {page, image} objects (PDF→images only) |
from_format |
string | Source format used |
to_format |
string | Target format used |
file_url |
string | Presigned download URL (when MinIO storage is enabled) |
metadata |
object | File stats: page_count, file_size, etc. |
Tip
The maximum accepted file size is 50 MB. For HTML-to-PDF use the html parameter instead of file to skip base64 encoding — this is the most convenient path for server-generated documents.