Home / Use cases / Convert a PDF to Markdown in Python

Updated

Convert a PDF to Markdown in Python (Headings and Tables Kept)

PyPDF and pdfminer give you text without structure: headings become plain lines and tables turn into loose words. Markdown keeps the structure that LLMs and search need.

Try it now on Apify Document to Markdown & JSON (PDF, DOCX, PPTX, HTML, OCR) · $0.004 per one document converted, first 50 pages included · failed items are free

How to do it, step by step

  1. pip install apify-client.
  2. Send the PDF URL(s) with the code below; local files can be uploaded to an Apify key-value store or any public URL first.
  3. Read markdown for the whole document, pages for per-page text and tables.
  4. Check ocrPages: pages without a text layer are read with OCR and charged separately.
from apify_client import ApifyClient
client = ApifyClient("YOUR_APIFY_TOKEN")
run = client.actor("plainprobe/document-to-markdown").call(
    run_input={"urls": ["https://arxiv.org/pdf/1810.04805"], "includePages": False})
doc = next(client.dataset(run["default_dataset_id"]).iterate_items())
open("bert.md", "w").write(doc["markdown"])

The exact input used for the example below:

{
  "urls": [
    "https://arxiv.org/pdf/1810.04805"
  ],
  "includePages": false
}

Real result

Run 4hh8iBHTF3zzugofH on 2026-12-05: status SUCCEEDED, 1 item(s), 6.1 s of run time. Output shortened for reading (long text cut with “…”).

“BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding”: 16 pages, 10,752 words, 0 OCR pages, converted in 2.6 s.

[
  {
    "url": "https://arxiv.org/pdf/1810.04805",
    "status": "ok",
    "title": "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding",
    "pageCount": 16,
    "wordCount": 10752,
    "ocrPages": 0,
    "markdown": "## BERT: Pre-training of Deep Bidirectional Transformers for\n\n## Language Understanding\n\nJacob Devlin Ming-Wei Chang\n\nKenton Lee Kristina Toutanova\n\nGoogle AI Language\n{jacobdevlin,mingweichang,kentonl,kristout}@google.com\n\nAbstract\n\nWe introduce a new language representation\nmodel called BERT, which stands for\nBidirectional Encoder Representations from\nTransformers. Unlike recent language represe…",
    "chunks": null
  }
]

Try it now with your own input

FAQ

Is there a free way?

Yes: libraries such as pymupdf4llm or markitdown run locally. Use the Actor when you have many files, scans that need OCR, or a server without heavy dependencies.

What about scanned PDFs?

Pages without text are read with Tesseract OCR automatically (ocr: auto) and reported in ocrPages.

Which other formats work?

DOCX, PPTX, HTML and images (PNG, JPEG, TIFF, read with OCR), with the same output fields.

Related