<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Markdown on</title><link>https://augmentedresilience.com/tags/markdown/</link><description>Recent content in Markdown on</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Sat, 02 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://augmentedresilience.com/tags/markdown/index.xml" rel="self" type="application/rss+xml"/><item><title>Upgrading My PDF Converter to IBM's Docling</title><link>https://augmentedresilience.com/posts/augmented-resilience-posts/upgrading-my-pdf-converter-to-ibm-docling/</link><pubDate>Sat, 02 May 2026 00:00:00 +0000</pubDate><guid>https://augmentedresilience.com/posts/augmented-resilience-posts/upgrading-my-pdf-converter-to-ibm-docling/</guid><description>&lt;h2 id="when-my-own-tool-couldnt-handle-my-work">When My Own Tool Couldn&amp;rsquo;t Handle My Work&lt;/h2>
&lt;p>The error message was easy to dismiss: &lt;code>RapidOCR returned empty result!&lt;/code>. It appeared twice in the terminal, then silence — a blank .md file where a 40-page Oracle HCM implementation guide should have been. The PDF had come straight from Oracle&amp;rsquo;s support portal, the same format I use for every triage session. But this one stored its pages as images, and PyMuPDF4LLM had nothing to work with.&lt;/p></description><content>&lt;h2 id="when-my-own-tool-couldnt-handle-my-work">When My Own Tool Couldn&amp;rsquo;t Handle My Work&lt;/h2>
&lt;p>The error message was easy to dismiss: &lt;code>RapidOCR returned empty result!&lt;/code>. It appeared twice in the terminal, then silence — a blank .md file where a 40-page Oracle HCM implementation guide should have been. The PDF had come straight from Oracle&amp;rsquo;s support portal, the same format I use for every triage session. But this one stored its pages as images, and PyMuPDF4LLM had nothing to work with.&lt;/p>
&lt;p>That was one category of failure. The other was quieter. For documents that did convert, I started noticing the tables were wrong — not corrupted, just structurally dissolved. An eligibility matrix that should have had six clearly labeled columns came back as a run of loosely connected text. Useful for nothing.&lt;/p>
&lt;p>I had built this tool to serve my Oracle work. Then my Oracle work showed me exactly where it fell short.&lt;/p>
&lt;hr>
&lt;h2 id="the-problem-with-pymupdf4llm">The Problem with PyMuPDF4LLM&lt;/h2>
&lt;p>If you&amp;rsquo;ve followed this series, you know that PyMuPDF4LLM was a solid choice when I first &lt;a href="https://augmentedresilience.com/posts/when-your-pdf-workflow-breaks-building-a-markdown-converter-with-claude-code/" target="_blank" rel="noopener noreferrer">built the converter&lt;/a>
. It handled text-based PDFs cleanly, installed without friction, and required almost no configuration. For research papers and simple documentation, it worked well.&lt;/p>
&lt;p>But Oracle HCM documentation is a different category of document. Oracle&amp;rsquo;s guides are dense with tables: configuration reference grids, eligibility matrices, step-and-action setup tables. These are not decorative — they carry most of the meaning. When PyMuPDF4LLM dissolved those tables into unstructured text, it was silently degrading the most important parts of the document.&lt;/p>
&lt;p>The image-based PDF problem was a hard wall. If a document was captured as page images rather than extractable text, the converter returned nothing. No partial output, no warning — just empty files.&lt;/p>
&lt;hr>
&lt;h2 id="discovering-docling">Discovering Docling&lt;/h2>
&lt;p>IBM Research Zurich&amp;rsquo;s AI for Knowledge team open-sourced &lt;a href="https://github.com/docling-project/docling" target="_blank" rel="noopener noreferrer">Docling&lt;/a>
in July 2024. The project has a specific focus: turning complex documents into structured, AI-ready output. In April 2025, IBM donated it to the Linux Foundation AI &amp;amp; Data, and it now powers data ingestion for Red Hat Enterprise Linux AI. As of this writing it has over 24,000 GitHub stars.&lt;/p>
&lt;p>What makes Docling different is that it treats document conversion as a computer vision problem, not just a text extraction problem.&lt;/p>
&lt;p>&lt;strong>Layout analysis:&lt;/strong> Docling uses an RT-DETR-derived model trained on DocLayNet — IBM&amp;rsquo;s human-annotated dataset of real-world documents — to detect and classify every region on the page: tables, figures, headers, footers, section titles, body text. It knows the structure before it extracts any content.&lt;/p>
&lt;p>&lt;strong>Table reconstruction:&lt;/strong> This is where Docling earns its place for Oracle documentation. It uses a vision transformer called TableFormer that predicts row/column structure and header roles directly from the page image. The result is a proper Markdown table, not a stream of cell values.&lt;/p>
&lt;p>&lt;strong>Image-based PDFs:&lt;/strong> For documents stored as page images, Docling integrates OCR into its pipeline natively. The same converter handles text-based and image-based PDFs without any changes on your end.&lt;/p>
&lt;hr>
&lt;h2 id="the-switch">The Switch&lt;/h2>
&lt;p>The API change was minimal. The old code:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> pymupdf4llm
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>md_text &lt;span style="color:#f92672">=&lt;/span> pymupdf4llm&lt;span style="color:#f92672">.&lt;/span>to_markdown(pdf_path)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The new code:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">from&lt;/span> docling.document_converter &lt;span style="color:#f92672">import&lt;/span> DocumentConverter
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>converter &lt;span style="color:#f92672">=&lt;/span> DocumentConverter()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>result &lt;span style="color:#f92672">=&lt;/span> converter&lt;span style="color:#f92672">.&lt;/span>convert(pdf_path)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>md_text &lt;span style="color:#f92672">=&lt;/span> result&lt;span style="color:#f92672">.&lt;/span>document&lt;span style="color:#f92672">.&lt;/span>export_to_markdown()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Three lines instead of one, but the extra structure pays dividends: &lt;code>DocumentConverter&lt;/code> can be initialized once and reused across an entire batch, which matters when processing a folder of 50 Oracle guides.&lt;/p>
&lt;p>&lt;strong>A note on startup:&lt;/strong> The first time you run Docling, it downloads its ML models from Hugging Face. You will see this:&lt;/p>
&lt;pre tabindex="0">&lt;code>Loading weights: 100%|██████████| 770/770 [00:00&amp;lt;00:00, 1656.35it/s]
&lt;/code>&lt;/pre>&lt;p>This is normal. The models cache locally after the first download and subsequent runs start immediately. If you see a warning about &lt;code>HF_TOKEN&lt;/code>, that is also expected — Docling works without one, but setting a token removes the rate-limit warning:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-zsh" data-lang="zsh">&lt;span style="display:flex;">&lt;span>echo &lt;span style="color:#e6db74">&amp;#39;export HF_TOKEN=&amp;#34;hf_your_token_here&amp;#34;&amp;#39;&lt;/span> &amp;gt;&amp;gt; ~/.zshrc
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="what-changed-in-practice">What Changed in Practice&lt;/h2>
&lt;p>&lt;strong>Oracle documentation:&lt;/strong> Tables that previously collapsed into text now render as proper Markdown tables. A 6-column configuration reference comes back with headers intact and every row correctly aligned.&lt;/p>
&lt;p>&lt;strong>AI books:&lt;/strong> My knowledge base includes dense technical books on LLM engineering and machine learning. These have complex layouts — sidebars, multi-column sections, figures with captions. Docling&amp;rsquo;s layout model handles these significantly better than PyMuPDF4LLM&amp;rsquo;s heuristic approach.&lt;/p>
&lt;p>&lt;strong>Image-based PDFs:&lt;/strong> Documents that previously produced empty output now convert cleanly. The two-step workaround (ocrmypdf → pdf2md) is no longer necessary for most cases.&lt;/p>
&lt;hr>
&lt;h2 id="two-other-improvements">Two Other Improvements&lt;/h2>
&lt;p>While I was updating the engine, I added two things that were overdue:&lt;/p>
&lt;p>&lt;strong>DOCX support.&lt;/strong> The converter now handles Word documents using pandoc as a backend. The same &lt;code>pdf2md&lt;/code> command works for both file types. This matters for Oracle support exports and study notes from my reMarkable.&lt;/p>
&lt;p>&lt;strong>Batch manifest.&lt;/strong> When processing a large folder, the converter now writes a manifest file tracking which files have been converted and their checksums. Re-running on the same folder skips files that haven&amp;rsquo;t changed. A &lt;code>--force&lt;/code> flag overrides this when you need a fresh conversion.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pdf2md --batch ~/oracle-pdfs/ &lt;span style="color:#75715e"># skips already-converted&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pdf2md --batch ~/oracle-pdfs/ --force &lt;span style="color:#75715e"># reconverts everything&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="whats-next">What&amp;rsquo;s Next&lt;/h2>
&lt;p>The web UI — which I added in the &lt;a href="https://augmentedresilience.com/posts/adding-a-web-ui-to-my-pdf-to-markdown-converter/" target="_blank" rel="noopener noreferrer">last post&lt;/a>
— has also been updated to use Docling. Drag a PDF onto it, click Convert, and the same deep-learning pipeline runs behind the scenes.&lt;/p>
&lt;p>The next thing I want to add is direct output to the Obsidian inbox. Right now the flow is: convert → download ZIP → move to vault. A toggle that sends output directly to &lt;code>~/projects/obsidian-vault/00-inbox/&lt;/code> would cut that manual step entirely.&lt;/p>
&lt;p>The tool is doing what I originally wanted: converting my Oracle documentation and AI library into clean, searchable Markdown. Docling is what makes that reliable for the documents that actually matter.&lt;/p></content></item><item><title>Adding a Web UI to My PDF to Markdown Converter</title><link>https://augmentedresilience.com/posts/augmented-resilience-posts/adding-a-web-ui-to-my-pdf-to-markdown-converter/</link><pubDate>Sat, 28 Mar 2026 00:00:00 +0000</pubDate><guid>https://augmentedresilience.com/posts/augmented-resilience-posts/adding-a-web-ui-to-my-pdf-to-markdown-converter/</guid><description>&lt;h2 id="the-promise-i-made-to-myself">The Promise I Made to Myself&lt;/h2>
&lt;p>In my last post about &lt;a href="https://augmentedresilience.com/posts/when-your-pdf-workflow-breaks-building-a-markdown-converter-with-claude-code/" target="_blank" rel="noopener noreferrer">building the PDF to Markdown converter&lt;/a>
, I listed some &amp;ldquo;what&amp;rsquo;s next&amp;rdquo; ideas at the end. One of them was:&lt;/p>
&lt;blockquote>
&lt;p>FastAPI wrapper: Create an HTTP API for web apps to use&lt;/p>&lt;/blockquote>
&lt;p>Well, I did it. And I went a step further — I built a full drag-and-drop web UI on top of it.&lt;/p>
&lt;p>The CLI still works exactly as before. This is an addition, not a replacement. But now when I want to convert a batch of PDFs without thinking about terminal commands, I just open a browser tab.&lt;/p></description><content>&lt;h2 id="the-promise-i-made-to-myself">The Promise I Made to Myself&lt;/h2>
&lt;p>In my last post about &lt;a href="https://augmentedresilience.com/posts/when-your-pdf-workflow-breaks-building-a-markdown-converter-with-claude-code/" target="_blank" rel="noopener noreferrer">building the PDF to Markdown converter&lt;/a>
, I listed some &amp;ldquo;what&amp;rsquo;s next&amp;rdquo; ideas at the end. One of them was:&lt;/p>
&lt;blockquote>
&lt;p>FastAPI wrapper: Create an HTTP API for web apps to use&lt;/p>&lt;/blockquote>
&lt;p>Well, I did it. And I went a step further — I built a full drag-and-drop web UI on top of it.&lt;/p>
&lt;p>The CLI still works exactly as before. This is an addition, not a replacement. But now when I want to convert a batch of PDFs without thinking about terminal commands, I just open a browser tab.&lt;/p>
&lt;p>&lt;img src="https://augmentedresilience.com/images/pdf-to-markdown-web-ui.png" alt="Image Description">&lt;/p>
&lt;hr>
&lt;h2 id="what-the-ui-does">What the UI Does&lt;/h2>
&lt;p>The interface is intentionally minimal:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Drag-and-drop zone&lt;/strong> — drop one PDF or fifty onto it&lt;/li>
&lt;li>&lt;strong>Browse button&lt;/strong> — if you prefer clicking&lt;/li>
&lt;li>&lt;strong>Convert button&lt;/strong> — kicks off the conversion&lt;/li>
&lt;li>&lt;strong>Per-file progress bars&lt;/strong> — live updates as each file converts&lt;/li>
&lt;li>&lt;strong>Individual download&lt;/strong> — each completed file gets its own Download button&lt;/li>
&lt;li>&lt;strong>Download all as ZIP&lt;/strong> — one click to grab everything&lt;/li>
&lt;li>&lt;strong>Clear&lt;/strong> — resets the session and cleans up temp files server-side&lt;/li>
&lt;/ul>
&lt;p>Everything runs locally. Files go to a temp directory on your machine, get converted, and are served back to you. Nothing hits an external API.&lt;/p>
&lt;hr>
&lt;h2 id="the-stack">The Stack&lt;/h2>
&lt;p>I kept it as simple as possible:&lt;/p>
&lt;p>&lt;strong>Backend:&lt;/strong> FastAPI + uvicorn&lt;/p>
&lt;p>FastAPI was the obvious choice — it handles file uploads cleanly, has first-class async support, and the &lt;code>python-multipart&lt;/code> library makes multi-file form handling trivial. The conversion logic is unchanged from the CLI: &lt;code>pymupdf4llm.to_markdown()&lt;/code> doing the heavy lifting.&lt;/p>
&lt;p>&lt;strong>Progress updates:&lt;/strong> Server-Sent Events (SSE)&lt;/p>
&lt;p>This is the part I found most interesting. When you hit Convert, the browser opens a persistent connection to &lt;code>/progress/{job_id}&lt;/code> and receives a stream of JSON events — one every 400ms — until the job finishes. No polling loop, no WebSocket complexity. SSE is perfect for this: unidirectional, simple, and built into every modern browser.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">async&lt;/span> &lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">event_stream&lt;/span>():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">while&lt;/span> &lt;span style="color:#66d9ef">True&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> data &lt;span style="color:#f92672">=&lt;/span> json&lt;span style="color:#f92672">.&lt;/span>dumps({&lt;span style="color:#e6db74">&amp;#34;progress&amp;#34;&lt;/span>: job[&lt;span style="color:#e6db74">&amp;#34;progress&amp;#34;&lt;/span>], &lt;span style="color:#e6db74">&amp;#34;done&amp;#34;&lt;/span>: job[&lt;span style="color:#e6db74">&amp;#34;done&amp;#34;&lt;/span>]})
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">yield&lt;/span> &lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;data: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>data&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#ae81ff">\n\n&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> job[&lt;span style="color:#e6db74">&amp;#34;done&amp;#34;&lt;/span>]:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">break&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">await&lt;/span> asyncio&lt;span style="color:#f92672">.&lt;/span>sleep(&lt;span style="color:#ae81ff">0.4&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">return&lt;/span> StreamingResponse(event_stream(), media_type&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;text/event-stream&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>On the frontend, consuming it is three lines:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-javascript" data-lang="javascript">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> &lt;span style="color:#a6e22e">eventSource&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">new&lt;/span> &lt;span style="color:#a6e22e">EventSource&lt;/span>(&lt;span style="color:#e6db74">`/progress/&lt;/span>&lt;span style="color:#e6db74">${&lt;/span>&lt;span style="color:#a6e22e">jobId&lt;/span>&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">`&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#a6e22e">eventSource&lt;/span>.&lt;span style="color:#a6e22e">onmessage&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">e&lt;/span> =&amp;gt; {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">const&lt;/span> { &lt;span style="color:#a6e22e">progress&lt;/span>, &lt;span style="color:#a6e22e">done&lt;/span> } &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#a6e22e">JSON&lt;/span>.&lt;span style="color:#a6e22e">parse&lt;/span>(&lt;span style="color:#a6e22e">e&lt;/span>.&lt;span style="color:#a6e22e">data&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">// update the UI...
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>};
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>Threading:&lt;/strong> The conversion itself is synchronous (PyMuPDF4LLM blocks while it processes pages). To keep the FastAPI event loop from freezing during conversion, each job runs in a &lt;code>ThreadPoolExecutor&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>asyncio&lt;span style="color:#f92672">.&lt;/span>get_event_loop()&lt;span style="color:#f92672">.&lt;/span>run_in_executor(executor, _convert_job, job_id, job_dir)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Four workers by default — enough to handle several simultaneous conversions without overloading the machine.&lt;/p>
&lt;p>&lt;strong>Frontend:&lt;/strong> Vanilla JS, no build step&lt;/p>
&lt;p>I deliberately avoided React, Vue, or any framework. The whole UI is a single &lt;code>static/index.html&lt;/code> file. It loads instantly, has no dependencies to install, and is easy to read and modify. For a local tool that one person uses, this is the right call.&lt;/p>
&lt;hr>
&lt;h2 id="project-structure">Project Structure&lt;/h2>
&lt;p>Here&amp;rsquo;s what changed from the original CLI project:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>pdf-to-markdown/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> pdf2md — original CLI (unchanged)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> app.py — FastAPI server (new)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> static/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> index.html — drag-drop UI (new)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> serve — start script (new)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> requirements.txt — updated with FastAPI deps
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> venv/ — existing venv, three new packages added
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The &lt;code>serve&lt;/code> script is just:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#!/usr/bin/env bash
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>cd &lt;span style="color:#e6db74">&amp;#34;&lt;/span>&lt;span style="color:#66d9ef">$(&lt;/span>dirname &lt;span style="color:#e6db74">&amp;#34;&lt;/span>$0&lt;span style="color:#e6db74">&amp;#34;&lt;/span>&lt;span style="color:#66d9ef">)&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>source venv/bin/activate
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>uvicorn app:app --host 0.0.0.0 --port &lt;span style="color:#ae81ff">8765&lt;/span> --reload
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Run it once, open &lt;code>http://localhost:8765&lt;/code>, and you have a working converter in your browser.&lt;/p>
&lt;hr>
&lt;h2 id="one-gotcha-pymupdf4llm-is-synchronous">One Gotcha: PyMuPDF4LLM is Synchronous&lt;/h2>
&lt;p>This tripped me up briefly. &lt;code>pymupdf4llm.to_markdown()&lt;/code> does not return a coroutine — it&amp;rsquo;s a blocking call that can take 10–30 seconds on a large document. If you call it directly in an async FastAPI route handler, you freeze the entire event loop while it runs. No other requests get handled. The SSE stream stops updating.&lt;/p>
&lt;p>The fix is the &lt;code>ThreadPoolExecutor&lt;/code> pattern above — push the blocking work off the event loop entirely. The async route returns immediately, the SSE stream keeps ticking, and the conversion runs in a thread pool where it belongs.&lt;/p>
&lt;hr>
&lt;h2 id="the-download-endpoints">The Download Endpoints&lt;/h2>
&lt;p>Three endpoints handle output:&lt;/p>
&lt;pre tabindex="0">&lt;code>GET /download/{job_id}/{filename} — single .md file
GET /download-all/{job_id} — all .md files as a ZIP
DELETE /job/{job_id} — clean up temp files
&lt;/code>&lt;/pre>&lt;p>The ZIP is built in memory using Python&amp;rsquo;s &lt;code>zipfile&lt;/code> module and streamed directly to the browser — no intermediate file on disk:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>buf &lt;span style="color:#f92672">=&lt;/span> io&lt;span style="color:#f92672">.&lt;/span>BytesIO()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">with&lt;/span> zipfile&lt;span style="color:#f92672">.&lt;/span>ZipFile(buf, &lt;span style="color:#e6db74">&amp;#34;w&amp;#34;&lt;/span>, zipfile&lt;span style="color:#f92672">.&lt;/span>ZIP_DEFLATED) &lt;span style="color:#66d9ef">as&lt;/span> zf:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> f &lt;span style="color:#f92672">in&lt;/span> md_files:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> zf&lt;span style="color:#f92672">.&lt;/span>write(f, f&lt;span style="color:#f92672">.&lt;/span>name)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>buf&lt;span style="color:#f92672">.&lt;/span>seek(&lt;span style="color:#ae81ff">0&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">return&lt;/span> StreamingResponse(buf, media_type&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;application/zip&amp;#34;&lt;/span>, &lt;span style="color:#f92672">...&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="what-this-unlocks">What This Unlocks&lt;/h2>
&lt;p>The CLI was already useful. The web UI adds a few things the CLI cannot easily do:&lt;/p>
&lt;p>&lt;strong>Non-terminal users.&lt;/strong> Anyone on my network can now use this converter by visiting &lt;code>http://my-machine:8765&lt;/code>. No Python knowledge required.&lt;/p>
&lt;p>&lt;strong>Bulk drop workflows.&lt;/strong> Dragging 20 PDFs from Finder into a browser window and clicking Convert is significantly faster than constructing a &lt;code>--batch&lt;/code> command with the right paths.&lt;/p>
&lt;p>&lt;strong>Visual feedback.&lt;/strong> The progress bars are not just cosmetic. For large PDFs that take 20–30 seconds, knowing the conversion is running (and roughly how far along it is) removes the anxiety of staring at a terminal cursor.&lt;/p>
&lt;hr>
&lt;h2 id="whats-next">What&amp;rsquo;s Next&lt;/h2>
&lt;p>The original roadmap item was &amp;ldquo;FastAPI wrapper.&amp;rdquo; That&amp;rsquo;s done. The next one I&amp;rsquo;m eyeing:&lt;/p>
&lt;p>&lt;strong>Auto-feed to Obsidian inbox.&lt;/strong> Right now the flow is: convert in the web UI, download the ZIP, unzip, move to Obsidian. I&amp;rsquo;d like to add a toggle: &amp;ldquo;Send output directly to &lt;code>~/projects/obsidian-vault/00-inbox/&lt;/code>&amp;rdquo; — one less manual step.&lt;/p>
&lt;p>That&amp;rsquo;s a small addition to the backend. Coming soon.&lt;/p>
&lt;hr>
&lt;h2 id="running-it">Running It&lt;/h2>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>cd ~/projects/pdf-to-markdown
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>./serve
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Open http://localhost:8765&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The first run installs nothing new — the three new packages (fastapi, uvicorn, python-multipart) are already in the venv. It just works.&lt;/p></content></item><item><title>When Your PDF Workflow Breaks - Building a Markdown Converter with Claude Code</title><link>https://augmentedresilience.com/posts/augmented-resilience-posts/building-a-pdf-to-markdown-converter-with-claude-code/</link><pubDate>Wed, 18 Feb 2026 00:00:00 +0000</pubDate><guid>https://augmentedresilience.com/posts/augmented-resilience-posts/building-a-pdf-to-markdown-converter-with-claude-code/</guid><description>&lt;h2 id="the-problem-pdfs-are-knowledge-prisons">The Problem: PDFs Are Knowledge Prisons&lt;/h2>
&lt;p>You know that feeling when you download a brilliant research paper, only to realize you can&amp;rsquo;t easily feed it into your AI workflow? Or when you want to add documentation to your knowledge base, but it&amp;rsquo;s locked in a format that doesn&amp;rsquo;t play well with version control or LLM tools?&lt;/p>
&lt;p>Yeah, I was there last week.&lt;/p>
&lt;p>I had just downloaded a fascinating 1.3MB research paper on Generative Engine Optimization and wanted to process it with my AI tools. But PDFs are terrible for this. They&amp;rsquo;re designed for &lt;em>printing&lt;/em>, not for &lt;em>processing&lt;/em>. What I needed was Markdown—clean, portable, AI-friendly Markdown.&lt;/p></description><content>&lt;h2 id="the-problem-pdfs-are-knowledge-prisons">The Problem: PDFs Are Knowledge Prisons&lt;/h2>
&lt;p>You know that feeling when you download a brilliant research paper, only to realize you can&amp;rsquo;t easily feed it into your AI workflow? Or when you want to add documentation to your knowledge base, but it&amp;rsquo;s locked in a format that doesn&amp;rsquo;t play well with version control or LLM tools?&lt;/p>
&lt;p>Yeah, I was there last week.&lt;/p>
&lt;p>I had just downloaded a fascinating 1.3MB research paper on Generative Engine Optimization and wanted to process it with my AI tools. But PDFs are terrible for this. They&amp;rsquo;re designed for &lt;em>printing&lt;/em>, not for &lt;em>processing&lt;/em>. What I needed was Markdown—clean, portable, AI-friendly Markdown.&lt;/p>
&lt;p>So I built a converter. And with Claude Code as my copilot through the PAI (Personal AI Infrastructure) system, the whole thing took less than 30 minutes.&lt;/p>
&lt;p>Here&amp;rsquo;s how it went down.&lt;/p>
&lt;hr>
&lt;h2 id="why-markdown-is-better-than-pdf-for-llms">Why Markdown is Better Than PDF for LLMs&lt;/h2>
&lt;p>Before diving into the build, let&amp;rsquo;s answer the obvious question: &lt;em>why bother converting?&lt;/em> Can&amp;rsquo;t LLMs just read PDFs directly?&lt;/p>
&lt;p>Technically, yes. But the results are significantly worse, and the reasons are fundamental to how PDFs work.&lt;/p>
&lt;h3 id="pdfs-are-layout-first-not-structure-first">PDFs Are Layout-First, Not Structure-First&lt;/h3>
&lt;p>PDFs were designed to describe &lt;em>where things appear on a page&lt;/em>, not &lt;em>what they mean&lt;/em>. As Steven Howard explains in &lt;a href="https://untetheredai.substack.com/p/why-pdfs-fail-under-llm-parsing" target="_blank" rel="noopener noreferrer">Why PDFs Fail Under LLM Parsing&lt;/a>
:&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Table cells with wrapped text insert hard line breaks that fragment token continuity and break logical row recognition. Headers and footers simply add noise to the context when used with LLMs. Sentences are split with arbitrary CR/LFs making it very difficult to find paragraph boundaries.&amp;rdquo;&lt;/p>&lt;/blockquote>
&lt;p>This architectural mismatch — a format designed for printing being fed into a system designed for understanding — causes cascading problems downstream.&lt;/p>
&lt;h3 id="the-token-efficiency-problem">The Token Efficiency Problem&lt;/h3>
&lt;p>Every token your LLM processes costs money and consumes context window space. PDF extraction wastes both.&lt;/p>
&lt;p>According to analysis from &lt;a href="https://markdownconverters.com/blog/pdf-vs-markdown-ai-tokens" target="_blank" rel="noopener noreferrer">MarkdownConverters&lt;/a>
, &lt;strong>Markdown saves up to 70% more tokens compared to extracted PDF text&lt;/strong> for the same content. The culprit: PDF extraction introduces formatting artifacts, metadata noise, headers/footers, and encoding remnants that all consume tokens without adding semantic value.&lt;/p>
&lt;p>To put that in practical terms: a PDF that would use 10,000 tokens might only need 3,000 tokens when properly converted to Markdown. At scale, this compounds dramatically.&lt;/p>
&lt;h3 id="the-rag-performance-problem">The RAG Performance Problem&lt;/h3>
&lt;p>If you&amp;rsquo;re building Retrieval Augmented Generation (RAG) systems — using documents as a knowledge base for AI — document format directly impacts answer quality.&lt;/p>
&lt;p>The research here is compelling:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;strong>Academic validation&lt;/strong>: A 2024 paper on arXiv (&lt;a href="https://arxiv.org/abs/2401.12599" target="_blank" rel="noopener noreferrer">Revolutionizing RAG with Enhanced PDF Structure Recognition&lt;/a>
) found that &amp;ldquo;the low accuracy of PDF parsing significantly impacts the effectiveness of professional knowledge-based QA.&amp;rdquo;&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Industry validation&lt;/strong>: NVIDIA&amp;rsquo;s technical blog documents how their NeMo Retriever pipeline converts extracted content to Markdown specifically because it &amp;ldquo;preserves row/column relationships in an LLM-native format, significantly reducing numeric hallucination&amp;rdquo; — and &lt;strong>reduces incorrect answers by 50%&lt;/strong>. (&lt;a href="https://developer.nvidia.com/blog/approaches-to-pdf-data-extraction-for-information-retrieval/" target="_blank" rel="noopener noreferrer">NVIDIA: Approaches to PDF Data Extraction for Information Retrieval&lt;/a>
)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Chunking quality&lt;/strong>: Analysis from &lt;a href="https://medium.com/data-science/improved-rag-document-processing-with-markdown-426a2e0dd82b" target="_blank" rel="noopener noreferrer">Towards Data Science&lt;/a>
shows that Markdown&amp;rsquo;s heading structure (&lt;code>#&lt;/code>, &lt;code>##&lt;/code>, &lt;code>###&lt;/code>) produces semantically meaningful chunks, while PDF-based chunking relies on arbitrary page breaks and heuristics.&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Retrieval failure rates&lt;/strong>: Unstructured.io&amp;rsquo;s &lt;a href="https://unstructured.io/blog/contextual-chunking-in-unstructured-platform-boost-your-rag-retrieval-accuracy" target="_blank" rel="noopener noreferrer">research on contextual chunking&lt;/a>
— tested across 5,563 question-answer pairs — showed an &lt;strong>84% reduction in retrieval failure rates&lt;/strong> when using structure-aware chunking (the kind Markdown enables natively).&lt;/p>
&lt;/li>
&lt;li>
&lt;p>&lt;strong>Real-world outcomes&lt;/strong>: The 2025 Semrush AI Index, cited by &lt;a href="https://developer.webex.com/blog/boosting-ai-performance-the-power-of-llm-friendly-content-in-markdown" target="_blank" rel="noopener noreferrer">Webex Developers Blog&lt;/a>
, found that 72% of top AI-indexed articles used Markdown or Markdown-like structures, achieving &lt;strong>34% higher retrieval accuracy&lt;/strong> across ChatGPT, Perplexity, and Gemini.&lt;/p>
&lt;/li>
&lt;/ul>
&lt;h3 id="the-bottom-line">The Bottom Line&lt;/h3>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Metric&lt;/th>
&lt;th>Impact&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Token reduction&lt;/td>
&lt;td>Up to 70% fewer tokens vs PDF extraction&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Incorrect answers in RAG&lt;/td>
&lt;td>50% reduction (NVIDIA NeMo)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Retrieval failure rates&lt;/td>
&lt;td>84% reduction (Unstructured.io)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Retrieval accuracy&lt;/td>
&lt;td>34% higher (Semrush AI Index 2025)&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Markdown isn&amp;rsquo;t just more convenient — it&amp;rsquo;s meaningfully better for AI. Converting your document libraries is one of the highest-ROI steps you can take before building any LLM-powered workflow.&lt;/p>
&lt;hr>
&lt;h2 id="the-first-failure-when-bleeding-edge-python-bites-back">The First Failure: When Bleeding-Edge Python Bites Back&lt;/h2>
&lt;p>I&amp;rsquo;m running Python 3.14.2—the latest release, barely a few weeks old. Modern, shiny, cutting-edge. Perfect, right?&lt;/p>
&lt;p>Not quite.&lt;/p>
&lt;p>My first instinct was to use &lt;code>marker-pdf&lt;/code>, a high-performance converter optimized for scientific papers and books. It looked perfect on paper (pun intended). But when I tried to install it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>Building wheel for Pillow (pyproject.toml): finished with status &amp;#39;error&amp;#39;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Ugh.&lt;/p>
&lt;p>Turns out, &lt;code>marker-pdf&lt;/code> depends on Pillow (the Python imaging library), and Pillow hasn&amp;rsquo;t built binary wheels for Python 3.14 yet. I could have downgraded Python. I could have fought with source compilation. But why?&lt;/p>
&lt;p>&lt;strong>This is where working with Claude Code really shines.&lt;/strong> Instead of going down a rabbit hole trying to force marker-pdf to work, Claude suggested pivoting to &lt;strong>PyMuPDF4LLM&lt;/strong>—a mature, actively maintained library specifically designed for AI/LLM workflows.&lt;/p>
&lt;p>And it just worked.&lt;/p>
&lt;hr>
&lt;h2 id="the-solution-pymupdf4llm">The Solution: PyMuPDF4LLM&lt;/h2>
&lt;p>PyMuPDF4LLM turned out to be exactly what I needed:&lt;/p>
&lt;ul>
&lt;li>Works flawlessly with Python 3.14 (no compilation errors)&lt;/li>
&lt;li>Fast and accurate conversion&lt;/li>
&lt;li>Built specifically for feeding documents into LLMs&lt;/li>
&lt;li>Clean, simple API&lt;/li>
&lt;li>Actively maintained by the PyMuPDF team&lt;/li>
&lt;/ul>
&lt;p>The installation was literally:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>pip install pymupdf4llm
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Five seconds later, I was ready to go.&lt;/p>
&lt;hr>
&lt;h2 id="building-the-tool-first-principles-thinking">Building the Tool: First Principles Thinking&lt;/h2>
&lt;p>As someone new to the CLI world, I&amp;rsquo;ve been learning to think through project structure from first principles. Where should this live? How should it be organized?&lt;/p>
&lt;p>With Claude&amp;rsquo;s guidance, I chose &lt;code>/Users/dsa/projects/pdf-to-markdown/&lt;/code> for a few key reasons:&lt;/p>
&lt;ol>
&lt;li>&lt;strong>Separation of Concerns:&lt;/strong> Tool projects should be separate from my main workspace&lt;/li>
&lt;li>&lt;strong>Discoverability:&lt;/strong> Clear, descriptive naming means I&amp;rsquo;ll find it again in 6 months&lt;/li>
&lt;li>&lt;strong>Reusability:&lt;/strong> This structure works both as a CLI tool AND as a library I could import later&lt;/li>
&lt;/ol>
&lt;p>The project structure ended up simple but complete:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>pdf-to-markdown/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── README.md # Documentation
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── venv/ # Isolated Python environment
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── input/ # Test PDFs
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── output/ # Generated markdown
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>├── pdf2md # CLI wrapper script
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>└── requirements.txt # Dependencies
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="the-code-a-simple-but-powerful-cli">The Code: A Simple but Powerful CLI&lt;/h2>
&lt;p>I wanted a tool I could actually use—something with a clean command-line interface that handles the common cases elegantly. Working with Claude through PAI, we created a Python script that does exactly that:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">#!/usr/bin/env python3&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74">&amp;#34;&amp;#34;&amp;#34;
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74">PDF to Markdown Converter
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74">A simple CLI tool to convert PDF files to Markdown using PyMuPDF4LLM
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#e6db74">&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> sys
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> os
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">from&lt;/span> pathlib &lt;span style="color:#f92672">import&lt;/span> Path
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> pymupdf4llm
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> pymupdf
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">from&lt;/span> tqdm &lt;span style="color:#f92672">import&lt;/span> tqdm
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">convert_pdf_to_markdown&lt;/span>(pdf_path: str, output_path: str &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>) &lt;span style="color:#f92672">-&amp;gt;&lt;/span> str:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&amp;#34;Convert a PDF file to Markdown format.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#f92672">not&lt;/span> os&lt;span style="color:#f92672">.&lt;/span>path&lt;span style="color:#f92672">.&lt;/span>exists(pdf_path):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">raise&lt;/span> &lt;span style="color:#a6e22e">FileNotFoundError&lt;/span>(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;PDF file not found: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>pdf_path&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e"># Get page count for progress bar&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> doc &lt;span style="color:#f92672">=&lt;/span> pymupdf&lt;span style="color:#f92672">.&lt;/span>open(pdf_path)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> page_count &lt;span style="color:#f92672">=&lt;/span> doc&lt;span style="color:#f92672">.&lt;/span>page_count
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> doc&lt;span style="color:#f92672">.&lt;/span>close()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Converting: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>pdf_path&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">with&lt;/span> tqdm(total&lt;span style="color:#f92672">=&lt;/span>page_count, unit&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;page&amp;#34;&lt;/span>, desc&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Processing&amp;#34;&lt;/span>, colour&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;blue&amp;#34;&lt;/span>) &lt;span style="color:#66d9ef">as&lt;/span> bar:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> md_text &lt;span style="color:#f92672">=&lt;/span> pymupdf4llm&lt;span style="color:#f92672">.&lt;/span>to_markdown(pdf_path, page_chunks&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#66d9ef">False&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> bar&lt;span style="color:#f92672">.&lt;/span>n &lt;span style="color:#f92672">=&lt;/span> page_count
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> bar&lt;span style="color:#f92672">.&lt;/span>refresh()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> output_path &lt;span style="color:#f92672">is&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_path &lt;span style="color:#f92672">=&lt;/span> Path(pdf_path)&lt;span style="color:#f92672">.&lt;/span>with_suffix(&lt;span style="color:#e6db74">&amp;#39;.md&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">with&lt;/span> open(output_path, &lt;span style="color:#e6db74">&amp;#39;w&amp;#39;&lt;/span>, encoding&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#39;utf-8&amp;#39;&lt;/span>) &lt;span style="color:#66d9ef">as&lt;/span> f:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> f&lt;span style="color:#f92672">.&lt;/span>write(md_text)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;✓ Done: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>output_path&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74"> (&lt;/span>&lt;span style="color:#e6db74">{&lt;/span>len(md_text)&lt;span style="color:#e6db74">:&lt;/span>&lt;span style="color:#e6db74">,&lt;/span>&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74"> characters)&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> str(output_path)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">batch_convert&lt;/span>(input_dir: str, output_dir: str &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>) &lt;span style="color:#f92672">-&amp;gt;&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&amp;#34;Convert all PDFs in a directory to Markdown.&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> input_path &lt;span style="color:#f92672">=&lt;/span> Path(input_dir)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#f92672">not&lt;/span> input_path&lt;span style="color:#f92672">.&lt;/span>is_dir():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">raise&lt;/span> &lt;span style="color:#a6e22e">NotADirectoryError&lt;/span>(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Not a directory: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>input_dir&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> pdfs &lt;span style="color:#f92672">=&lt;/span> sorted(input_path&lt;span style="color:#f92672">.&lt;/span>glob(&lt;span style="color:#e6db74">&amp;#34;*.pdf&amp;#34;&lt;/span>))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#f92672">not&lt;/span> pdfs:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;No PDF files found in: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>input_dir&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> sys&lt;span style="color:#f92672">.&lt;/span>exit(&lt;span style="color:#ae81ff">0&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> output_dir:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_dir &lt;span style="color:#f92672">=&lt;/span> Path(output_dir)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">else&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_dir &lt;span style="color:#f92672">=&lt;/span> input_path&lt;span style="color:#f92672">.&lt;/span>parent &lt;span style="color:#f92672">/&lt;/span> &lt;span style="color:#e6db74">&amp;#34;output&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_dir&lt;span style="color:#f92672">.&lt;/span>mkdir(parents&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#66d9ef">True&lt;/span>, exist_ok&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#66d9ef">True&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> total &lt;span style="color:#f92672">=&lt;/span> len(pdfs)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> succeeded &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> failed &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>&lt;span style="color:#ae81ff">\n&lt;/span>&lt;span style="color:#e6db74">Batch mode: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>total&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74"> PDF(s) found in &amp;#39;&lt;/span>&lt;span style="color:#e6db74">{&lt;/span>input_dir&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#39;&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Output folder: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>output_dir&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#ae81ff">\n&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> i, pdf_path &lt;span style="color:#f92672">in&lt;/span> enumerate(pdfs, start&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;[&lt;/span>&lt;span style="color:#e6db74">{&lt;/span>i&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">/&lt;/span>&lt;span style="color:#e6db74">{&lt;/span>total&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">] &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>pdf_path&lt;span style="color:#f92672">.&lt;/span>name&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_path &lt;span style="color:#f92672">=&lt;/span> output_dir &lt;span style="color:#f92672">/&lt;/span> pdf_path&lt;span style="color:#f92672">.&lt;/span>with_suffix(&lt;span style="color:#e6db74">&amp;#39;.md&amp;#39;&lt;/span>)&lt;span style="color:#f92672">.&lt;/span>name
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">try&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> convert_pdf_to_markdown(str(pdf_path), str(output_path))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> succeeded &lt;span style="color:#f92672">+=&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">except&lt;/span> &lt;span style="color:#a6e22e">Exception&lt;/span> &lt;span style="color:#66d9ef">as&lt;/span> e:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34; ✗ Failed: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>e&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> failed &lt;span style="color:#f92672">+=&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;─&amp;#34;&lt;/span> &lt;span style="color:#f92672">*&lt;/span> &lt;span style="color:#ae81ff">40&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Batch complete: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>succeeded&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74"> converted, &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>failed&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74"> failed&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">f&lt;/span>&lt;span style="color:#e6db74">&amp;#34;Output folder: &lt;/span>&lt;span style="color:#e6db74">{&lt;/span>output_dir&lt;span style="color:#e6db74">}&lt;/span>&lt;span style="color:#e6db74">&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;&amp;#34;&amp;#34;Main CLI entry point&amp;#34;&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> args &lt;span style="color:#f92672">=&lt;/span> sys&lt;span style="color:#f92672">.&lt;/span>argv[&lt;span style="color:#ae81ff">1&lt;/span>:]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#f92672">not&lt;/span> args:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;Usage:&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md &amp;lt;input.pdf&amp;gt; [output.md] # Convert a single PDF&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md --batch &amp;lt;folder/&amp;gt; # Convert all PDFs in a folder&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md --batch &amp;lt;folder/&amp;gt; --output &amp;lt;out_folder/&amp;gt; # Batch with custom output dir&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;&lt;/span>&lt;span style="color:#ae81ff">\n&lt;/span>&lt;span style="color:#e6db74">Examples:&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md document.pdf # Creates document.md&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md document.pdf custom.md # Creates custom.md&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md --batch input/ # Converts all PDFs in input/&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> print(&lt;span style="color:#e6db74">&amp;#34; pdf2md --batch ~/documents/pdfs/ --output ~/knowledge-base/docs/&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> sys&lt;span style="color:#f92672">.&lt;/span>exit(&lt;span style="color:#ae81ff">1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> args[&lt;span style="color:#ae81ff">0&lt;/span>] &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#34;--batch&amp;#34;&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> input_dir &lt;span style="color:#f92672">=&lt;/span> args[&lt;span style="color:#ae81ff">1&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_dir &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#e6db74">&amp;#34;--output&amp;#34;&lt;/span> &lt;span style="color:#f92672">in&lt;/span> args:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> idx &lt;span style="color:#f92672">=&lt;/span> args&lt;span style="color:#f92672">.&lt;/span>index(&lt;span style="color:#e6db74">&amp;#34;--output&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_dir &lt;span style="color:#f92672">=&lt;/span> args[idx &lt;span style="color:#f92672">+&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> batch_convert(input_dir, output_dir)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">else&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> pdf_path &lt;span style="color:#f92672">=&lt;/span> args[&lt;span style="color:#ae81ff">0&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> output_path &lt;span style="color:#f92672">=&lt;/span> args[&lt;span style="color:#ae81ff">1&lt;/span>] &lt;span style="color:#66d9ef">if&lt;/span> len(args) &lt;span style="color:#f92672">&amp;gt;&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span> &lt;span style="color:#66d9ef">else&lt;/span> &lt;span style="color:#66d9ef">None&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> convert_pdf_to_markdown(pdf_path, output_path)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">if&lt;/span> __name__ &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#e6db74">&amp;#34;__main__&amp;#34;&lt;/span>:
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> main()
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>What I love about this code:&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Smart defaults:&lt;/strong> If you don&amp;rsquo;t specify an output path, it just replaces &lt;code>.pdf&lt;/code> with &lt;code>.md&lt;/code>&lt;/li>
&lt;li>&lt;strong>Progress bars:&lt;/strong> &lt;code>tqdm&lt;/code> gives you a blue progress bar with page count&lt;/li>
&lt;li>&lt;strong>Batch mode:&lt;/strong> &lt;code>--batch&lt;/code> processes an entire folder at once, with optional &lt;code>--output&lt;/code> target&lt;/li>
&lt;li>&lt;strong>Helpful errors:&lt;/strong> Clear messages when things go wrong&lt;/li>
&lt;li>&lt;strong>Flexible usage:&lt;/strong> Works with relative paths, absolute paths, custom output names&lt;/li>
&lt;/ul>
&lt;p>Make it executable:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>chmod +x pdf2md
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>And now it&amp;rsquo;s a proper command-line tool.&lt;/p>
&lt;hr>
&lt;h2 id="the-moment-of-truth-testing-with-real-data">The Moment of Truth: Testing with Real Data&lt;/h2>
&lt;p>Theory is great. But does it actually work?&lt;/p>
&lt;p>I grabbed that 1.3MB research paper on Generative Engine Optimization and ran:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>python pdf2md input/test.pdf output/test.md
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The output:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-text" data-lang="text">&lt;span style="display:flex;">&lt;span>Converting input/test.pdf to Markdown...
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Processing: 100%|████████████████| 12/12 [00:02&amp;lt;00:00, 5.8 pages/s]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>✓ Done: output/test.md (73,463 characters)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;strong>1.3MB PDF → 74KB of clean Markdown in seconds.&lt;/strong>&lt;/p>
&lt;p>I opened the output file, and there it was—perfectly formatted markdown:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-markdown" data-lang="markdown">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">## **GEO: Generative Engine Optimization**
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Pranjal Aggarwal [∗]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Indian Institute of Technology Delhi
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>New Delhi, India
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pranjal2041@gmail.com
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Ashwin Kalyan
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Independent
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>Seattle, USA
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>asaavashwin@gmail.com
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>...
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Headers, formatting, structure—all preserved. No manual cleanup needed.&lt;/p>
&lt;p>Success.&lt;/p>
&lt;hr>
&lt;h2 id="what-this-unlocks">What This Unlocks&lt;/h2>
&lt;p>Now that I have PDFs converting to Markdown reliably, a whole world of possibilities opens up:&lt;/p>
&lt;h3 id="ai-workflows">AI Workflows&lt;/h3>
&lt;ul>
&lt;li>Feed research papers and documentation directly into Claude or other LLMs&lt;/li>
&lt;li>Build RAG (Retrieval Augmented Generation) pipelines backed by your document library&lt;/li>
&lt;li>Process technical documentation at scale without losing structure&lt;/li>
&lt;/ul>
&lt;h3 id="knowledge-management">Knowledge Management&lt;/h3>
&lt;ul>
&lt;li>Import PDFs into your Obsidian vault automatically&lt;/li>
&lt;li>Version control document content (because it&amp;rsquo;s now plain text in git)&lt;/li>
&lt;li>Full-text search across your entire converted document library&lt;/li>
&lt;/ul>
&lt;h3 id="automation-ideas">Automation Ideas&lt;/h3>
&lt;ul>
&lt;li>Watch folder that auto-converts any dropped PDFs&lt;/li>
&lt;li>Batch process entire directories of reports, papers, or manuals&lt;/li>
&lt;li>Feed converted markdown directly into a vector database&lt;/li>
&lt;li>API wrapper to convert PDFs via HTTP requests&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="lessons-learned-especially-for-cli-beginners">Lessons Learned (Especially for CLI Beginners)&lt;/h2>
&lt;h3 id="1-virtual-environments-are-non-negotiable">1. Virtual Environments Are Non-Negotiable&lt;/h3>
&lt;p>Every Python project should live in its own virtual environment. Always:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>python3 -m venv venv
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>source venv/bin/activate
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pip install --upgrade pip
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This keeps dependencies isolated and projects reproducible.&lt;/p>
&lt;h3 id="2-bleeding-edge-isnt-always-better">2. Bleeding-Edge Isn&amp;rsquo;t Always Better&lt;/h3>
&lt;p>Python 3.14 is awesome, but sometimes mature tooling (like PyMuPDF) that &amp;ldquo;just works&amp;rdquo; beats bleeding-edge alternatives. Don&amp;rsquo;t be afraid to pivot when something doesn&amp;rsquo;t work.&lt;/p>
&lt;h3 id="3-test-with-real-data">3. Test With Real Data&lt;/h3>
&lt;p>I didn&amp;rsquo;t test with &amp;ldquo;hello.pdf&amp;rdquo; containing two sentences. I tested with a 1.3MB research paper. Real data reveals real issues (or in this case, confirms it works beautifully).&lt;/p>
&lt;h3 id="4-document-as-you-build">4. Document As You Build&lt;/h3>
&lt;p>Writing the README alongside the code made the project immediately understandable. Future-me will thank present-me.&lt;/p>
&lt;h3 id="5-claude-code--pai--superpowers">5. Claude Code + PAI = Superpowers&lt;/h3>
&lt;p>Working with Claude through the PAI infrastructure meant I had a senior developer helping me think through:&lt;/p>
&lt;ul>
&lt;li>Project structure (first principles)&lt;/li>
&lt;li>Library selection (when to pivot)&lt;/li>
&lt;li>Code organization (clean, maintainable)&lt;/li>
&lt;li>Real-world usage patterns&lt;/li>
&lt;/ul>
&lt;p>This wasn&amp;rsquo;t just coding faster—it was learning better patterns while building.&lt;/p>
&lt;hr>
&lt;h2 id="usage-examples">Usage Examples&lt;/h2>
&lt;h3 id="basic-conversion">Basic Conversion&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Activate environment first (always!)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>source venv/bin/activate
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Convert a PDF&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>python pdf2md document.pdf
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Custom output name&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>python pdf2md research.pdf my-notes.md
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Full paths&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>python pdf2md ~/Downloads/paper.pdf ~/Documents/notes.md
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="batch-processing">Batch Processing&lt;/h3>
&lt;p>Convert an entire folder of PDFs:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>source venv/bin/activate
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Convert all PDFs in a folder (output goes to output/ by default)&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>python pdf2md --batch ~/documents/pdfs/
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Convert to a specific knowledge base directory&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>python pdf2md --batch ~/documents/pdfs/ --output ~/knowledge-base/docs/
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="add-to-path-optional">Add to PATH (Optional)&lt;/h3>
&lt;p>To use &lt;code>pdf2md&lt;/code> from anywhere:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Add to ~/.zshrc&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>export PATH&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#34;/Users/dsa/projects/pdf-to-markdown:&lt;/span>$PATH&lt;span style="color:#e6db74">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e"># Then run from anywhere&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>pdf2md ~/Downloads/paper.pdf ~/Documents/paper.md
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;h2 id="whats-next">What&amp;rsquo;s Next?&lt;/h2>
&lt;p>This tool works great as-is, but there are some exciting enhancements on the roadmap:&lt;/p>
&lt;h3 id="immediate-improvements">Immediate Improvements&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Better layout analysis:&lt;/strong> Install &lt;code>pymupdf_layout&lt;/code> for improved structure detection on complex documents&lt;/li>
&lt;li>&lt;strong>Recursive batch mode:&lt;/strong> Process nested folder structures, not just flat directories&lt;/li>
&lt;/ul>
&lt;h3 id="future-integrations">Future Integrations&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>RAG pipeline:&lt;/strong> Auto-feed converted markdown into a vector database&lt;/li>
&lt;li>&lt;strong>Obsidian plugin:&lt;/strong> Detect PDFs in vault and convert automatically&lt;/li>
&lt;li>&lt;strong>FastAPI wrapper:&lt;/strong> Create an HTTP API for web apps to use&lt;/li>
&lt;li>&lt;strong>Electron/Tauri app:&lt;/strong> Build a desktop GUI for non-technical users&lt;/li>
&lt;/ul>
&lt;hr>
&lt;h2 id="the-bigger-picture-why-this-matters">The Bigger Picture: Why This Matters&lt;/h2>
&lt;p>This project is tiny—roughly 100 lines of Python, 30 minutes of work. But it represents something bigger:&lt;/p>
&lt;p>&lt;strong>The ability to build tools that solve your actual problems.&lt;/strong>&lt;/p>
&lt;p>I had a workflow friction (PDFs don&amp;rsquo;t work well with AI tools). I built a solution. Now that friction is gone, and I can focus on higher-level work.&lt;/p>
&lt;p>And the data is clear: converting your document library to Markdown isn&amp;rsquo;t a nice-to-have. It&amp;rsquo;s a multiplier on every AI workflow that follows. Up to 70% fewer tokens consumed. 84% fewer retrieval failures. 50% fewer incorrect answers. These aren&amp;rsquo;t marginal improvements—they&amp;rsquo;re transformational.&lt;/p>
&lt;p>Working with Claude Code through PAI accelerated all of this. It&amp;rsquo;s like having a patient senior developer sitting next to you, suggesting better approaches, catching errors before they happen, and explaining &lt;em>why&lt;/em> certain patterns work.&lt;/p>
&lt;hr>
&lt;h2 id="resources">Resources&lt;/h2>
&lt;ul>
&lt;li>&lt;strong>PyMuPDF4LLM Docs:&lt;/strong> &lt;a href="https://pymupdf.readthedocs.io/en/latest/pymupdf4llm/" target="_blank" rel="noopener noreferrer">https://pymupdf.readthedocs.io/en/latest/pymupdf4llm/&lt;/a>
&lt;/li>
&lt;li>&lt;strong>PyMuPDF GitHub:&lt;/strong> &lt;a href="https://github.com/pymupdf/PyMuPDF" target="_blank" rel="noopener noreferrer">https://github.com/pymupdf/PyMuPDF&lt;/a>
&lt;/li>
&lt;/ul>
&lt;h3 id="citations-markdown-vs-pdf-for-llms">Citations: Markdown vs PDF for LLMs&lt;/h3>
&lt;ul>
&lt;li>&lt;strong>Why PDFs Fail Under LLM Parsing&lt;/strong> — Steven Howard, Untethered AI: &lt;a href="https://untetheredai.substack.com/p/why-pdfs-fail-under-llm-parsing" target="_blank" rel="noopener noreferrer">https://untetheredai.substack.com/p/why-pdfs-fail-under-llm-parsing&lt;/a>
&lt;/li>
&lt;li>&lt;strong>PDF vs Markdown for AI: Token Efficiency&lt;/strong> — MarkdownConverters: &lt;a href="https://markdownconverters.com/blog/pdf-vs-markdown-ai-tokens" target="_blank" rel="noopener noreferrer">https://markdownconverters.com/blog/pdf-vs-markdown-ai-tokens&lt;/a>
&lt;/li>
&lt;li>&lt;strong>Revolutionizing RAG with Enhanced PDF Structure Recognition&lt;/strong> — arXiv:2401.12599 (2024): &lt;a href="https://arxiv.org/abs/2401.12599" target="_blank" rel="noopener noreferrer">https://arxiv.org/abs/2401.12599&lt;/a>
&lt;/li>
&lt;li>&lt;strong>Approaches to PDF Data Extraction for Information Retrieval&lt;/strong> — NVIDIA Technical Blog: &lt;a href="https://developer.nvidia.com/blog/approaches-to-pdf-data-extraction-for-information-retrieval/" target="_blank" rel="noopener noreferrer">https://developer.nvidia.com/blog/approaches-to-pdf-data-extraction-for-information-retrieval/&lt;/a>
&lt;/li>
&lt;li>&lt;strong>Improved RAG Document Processing With Markdown&lt;/strong> — Dr. Leon Eversberg, Towards Data Science: &lt;a href="https://medium.com/data-science/improved-rag-document-processing-with-markdown-426a2e0dd82b" target="_blank" rel="noopener noreferrer">https://medium.com/data-science/improved-rag-document-processing-with-markdown-426a2e0dd82b&lt;/a>
&lt;/li>
&lt;li>&lt;strong>Contextual Chunking: Boost Your RAG Retrieval Accuracy&lt;/strong> — Unstructured.io: &lt;a href="https://unstructured.io/blog/contextual-chunking-in-unstructured-platform-boost-your-rag-retrieval-accuracy" target="_blank" rel="noopener noreferrer">https://unstructured.io/blog/contextual-chunking-in-unstructured-platform-boost-your-rag-retrieval-accuracy&lt;/a>
&lt;/li>
&lt;li>&lt;strong>Boosting AI Performance: The Power of LLM-Friendly Content in Markdown&lt;/strong> — Webex Developers Blog: &lt;a href="https://developer.webex.com/blog/boosting-ai-performance-the-power-of-llm-friendly-content-in-markdown" target="_blank" rel="noopener noreferrer">https://developer.webex.com/blog/boosting-ai-performance-the-power-of-llm-friendly-content-in-markdown&lt;/a>
&lt;/li>
&lt;/ul>
&lt;hr>
&lt;p>&lt;strong>Happy converting!&lt;/strong>&lt;/p></content></item></channel></rss>