How to convert a PDF to Markdown in Python
For files on your own disk, install pymupdf4llm and call pymupdf4llm.to_markdown("file.pdf"). For PDFs that are already online, or code that runs in a serverless function or an agent, POST the link to a PDF-to-Markdown API with requests and read the markdown field. Our page reader API costs $0.02 per file, needs no API key, and you can try it free 5 times a day.
Try it free Read Any Web Page, PDF or Doc — clean Markdown for AI agents · $0.02 / call
Option 1: a local library
pymupdf4llm is built on PyMuPDF and turns text PDFs into Markdown with headings and tables. It is fast and free, and the file never leaves your machine. marker is slower and heavier (it uses ML models) but handles complex layouts and scientific papers better.
pip install pymupdf4llm
import pymupdf4llm
md = pymupdf4llm.to_markdown("report.pdf")
open("report.md", "w", encoding="utf-8").write(md)
Option 2: the page reader API with requests
import base64, json, requests
URL = "https://iamai.markets/api/x402/services/XyehnOmhOzAF"
body = {"url": "https://example.com/report.pdf", "max_chars": 50000}
r = requests.post(URL, json=body, timeout=60)
if r.status_code == 402:
offer = json.loads(base64.b64decode(r.headers["PAYMENT-REQUIRED"]))["accepts"][0]
print("Price in USDC base units (6 decimals):", offer["amount"])
# Send the same request through your x402 v2 client. It signs the
# payment and repeats it with the PAYMENT-SIGNATURE header.
# The paid answer looks like {"transaction": "0x...", "price_usd": 0.02, "result": {...}}
def save(paid_json, path="report.md"):
doc = paid_json["result"]
if doc["truncated"]:
print("Longer than max_chars; raise it (up to 200000)")
open(path, "w", encoding="utf-8").write(doc["markdown"])
return doc["word_count"]
Paying from Python
requests on its own only gets the 402 answer. To pay, route the call through any x402 v2 client, for example Coinbase's x402 packages or the iamai-agentkit plugin for Coinbase AgentKit, with a wallet that holds USDC on Base. If you only want to check the output on a few files first, use the free tries on the listing page.
Errors worth handling
- 402 again after paying, with an error field: the payment was rejected and nothing was charged.
- 502 with charged: false: the file could not be read (missing, over 10 MB, site down). Nothing was charged.
- 429: more than 120 calls a minute. Slow down and retry.
- truncated: true in the result: the text was cut at max_chars.
Which one to pick
| Situation | Use |
|---|---|
| Private files on your own disk | pymupdf4llm |
| Complex layouts, papers with formulas | marker |
| PDFs already online, or Word, Excel and web pages too | Page reader API |
| Serverless code or an agent with no heavy dependencies | Page reader API |
The product
Questions
Can I send a local file to the API?
No, it takes a link. Upload the file somewhere that gives a direct link, such as a storage bucket with a signed URL, and send that.
Does it work with async code?
Yes. It is a normal HTTPS POST with a JSON body, so httpx or aiohttp work the same way as requests.
What timeout should I set?
About 60 seconds. Most PDFs come back much faster; the site has 10 seconds to send the file.
Related
Updated 2026-12-01
