PDFtoMD
DevelopersCLIbatchPandoc

Batch Convert PDF to Markdown from the Command Line

Convert an entire folder of PDFs to Markdown in one command. A practical guide to command line tools like Pandoc and Marker, plus batch scripts that scale to hundreds of files.

9 min readBy Rafael Abellan

Converting a single PDF to Markdown is a solved problem. You drag one file into a converter, wait a few seconds, and copy the result. But the moment you have a folder with fifty invoices, three hundred research papers, or an entire archive of old reports, that one-file-at-a-time workflow falls apart. Clicking through hundreds of files by hand is slow, error prone, and impossible to repeat reliably.

This is where the command line shines. With a single terminal command, you can walk through a directory, convert every PDF it contains, and write a clean Markdown file next to each one. You can schedule it, log it, and rerun it whenever new files arrive. This guide covers the practical batch tools, from Pandoc and Marker to plain shell loops, along with scripts you can copy and adapt today.

Why Batch Conversion Needs the Command Line

A graphical converter is built around one file and one human clicking a button. That is fine for occasional use. But batch work has different requirements: it must be unattended, deterministic, and easy to integrate with the rest of your tooling. The command line gives you all three.

  • Repeatability: A script produces the same result every time you run it. No forgotten files, no inconsistent settings.
  • Scale: Whether you have ten files or ten thousand, the command is the same. The computer does the tedious part.
  • Automation: A command line workflow can run on a schedule, trigger on a new file, or plug into a larger pipeline.
  • Composability: You can pipe the output into other tools, filter it, rename files, and organize results, all in the same script.

The core pattern for every batch job is the same: find the files, loop over them, convert each one, and write the output somewhere sensible. Once you understand that shape, the specific tool you choose is just a detail you can swap out.

Option 1: Pandoc for Structured, Text-Based PDFs

Pandoc is the universal document converter that many developers already have installed. It is fast, scriptable, and excellent at handling documents that already contain a real text layer. On most systems you can install it with a single command:

# macOS
brew install pandoc

# Debian or Ubuntu
sudo apt-get install pandoc

# Windows (with Chocolatey)
choco install pandoc

One important caveat: Pandoc does not read PDF as an input format directly, because PDF is a layout format rather than a document format. The common approach is to extract text first with a helper like pdftotext (from the Poppler utilities) and then let Pandoc normalize the result into Markdown. Here is a single-file conversion:

# Extract text while preserving layout, then convert to Markdown
pdftotext -layout report.pdf report.txt
pandoc report.txt -o report.md

To turn that into a batch job, wrap it in a shell loop that walks every PDF in the current folder. The snippet below converts each file and writes a matching .md file next to it:

#!/usr/bin/env bash
set -euo pipefail

for f in *.pdf; do
  name="${f%.pdf}"
  echo "Converting: $f"
  pdftotext -layout "$f" "$name.txt"
  pandoc "$name.txt" -o "$name.md"
  rm "$name.txt"
done

echo "Done."

The set -euo pipefail line makes the script stop on the first error instead of silently skipping files. The ${f%.pdf} expression strips the extension so report.pdf becomes report.md. Pandoc is the right choice when your PDFs are digital-native and text-heavy: contracts, manuals, ebooks, and exported documents. It is fast and has no heavy dependencies.

Where Pandoc struggles is with complex layouts, multi-column pages, and especially scanned documents, because there is no real text to extract. For those, you need a tool that understands the visual structure of the page.

Option 2: Marker for Complex Layouts and Scanned Documents

Marker is a modern, open-source tool built specifically to convert PDFs into clean Markdown. Unlike the extract-then-convert approach, it uses machine learning models to understand page structure, so it handles multi-column layouts, tables, headings, and even scanned pages far better than a plain text extractor. It is a Python package:

pip install marker-pdf

The headline feature for our purposes is that Marker ships with a built-in batch command. You point it at a folder of PDFs and an output folder, and it processes everything:

# Convert every PDF in ./input into Markdown in ./output
marker ./input --output_dir ./output

# Limit how many files run at once to control memory use
marker ./input --output_dir ./output --workers 4

Marker creates a subfolder per document containing the Markdown plus any extracted images, which is ideal when your PDFs contain figures or diagrams you want to keep. The tradeoff is resource usage. Because it runs ML models, Marker is much slower than Pandoc and benefits enormously from a GPU. On a laptop CPU, a few hundred pages can take a while, so it is best suited to overnight or scheduled jobs rather than instant conversions.

A good rule of thumb: reach for Pandoc when speed matters and the text is clean, and reach for Marker when accuracy on messy or scanned documents matters more than raw throughput. If you want a wider view of the landscape, our roundup of the best PDF to Markdown converters compared breaks down where each tool wins.

Skip the setup. Convert a folder of PDFs to clean Markdown in seconds, no install required.

Option 3: A Portable Shell Loop for Any Converter

The batch pattern is not tied to one tool. Once you have any command that converts a single PDF, you can wrap it in a loop and process an entire tree of folders. The example below uses find so it descends into subdirectories, which is exactly what you want for a deep archive:

#!/usr/bin/env bash
set -euo pipefail

# Recursively find every PDF under ./docs and convert it in place
find ./docs -type f -name "*.pdf" -print0 | while IFS= read -r -d '' f; do
  out="${f%.pdf}.md"
  if [ -f "$out" ]; then
    echo "Skipping (already done): $f"
    continue
  fi
  echo "Converting: $f"
  pdftotext -layout "$f" - | pandoc -f markdown -t gfm -o "$out"
done

Two details make this script robust. First, -print0 paired with read -r -d '' handles filenames with spaces or unusual characters, which is the single most common cause of broken batch scripts. Second, the check for an existing .md file means you can rerun the script safely: it skips work that is already done and only converts new files. That idempotency is what turns a one-off command into something you can schedule.

Running Conversions in Parallel

A sequential loop converts one file at a time, which wastes the extra cores on a modern machine. GNU parallel or even xargs can run several conversions at once and cut wall-clock time dramatically:

# Convert up to 4 PDFs at the same time
find ./docs -name "*.pdf" -print0 \
  | xargs -0 -P 4 -I {} sh -c 'pdftotext -layout "{}" "{}.txt"'

Parallelism helps most with CPU-light tools like Pandoc. With heavier ML tools, run fewer workers so you do not exhaust memory. Start with a small number, watch your system monitor, and increase only if you have headroom.

Handling the Hard Cases

Batch jobs fail in predictable ways. Planning for them up front saves hours of debugging a half-finished run.

Scanned and Image-Only PDFs

If a PDF is a scan, there is no text to extract and Pandoc will produce empty or garbage output. You need optical character recognition first. Tools like ocrmypdf add a text layer to a scanned file so downstream tools can read it. Our guide on how to convert scanned and image PDFs to Markdown with OCR covers this in depth, or you can let a tool like Marker handle OCR internally.

Tables and Multi-Column Layouts

Tables are the first thing that breaks in any conversion. Plain text extraction flattens columns into a jumble, while structure-aware tools preserve them as real Markdown tables. If your documents are table-heavy, test on a representative sample before committing to a tool for the whole batch.

Logging and Error Recovery

For a large batch, always write a log so you know what succeeded and what failed. Redirect output to a file and record failures explicitly:

for f in *.pdf; do
  if pdftotext -layout "$f" "${f%.pdf}.txt" 2>> errors.log; then
    echo "OK: $f" >> convert.log
  else
    echo "FAILED: $f" >> convert.log
  fi
done

When the run finishes, a quick grep FAILED convert.log tells you exactly which files need attention, so you can retry just those instead of running the entire batch again.

When to Use a Hosted Converter Instead

Command line tools are ideal when you control the machine and want everything to stay local. But they come with real costs: you install and update dependencies, you troubleshoot OCR models, and you own the quality of the output. For many teams, the maintenance is not worth it, especially when the documents are complex.

A hosted service like PDFtoMD removes that burden. It produces clean Markdown with correct heading hierarchy, preserved lists, and intact tables, without any local setup. You can start on the free tier with 3 conversions per month and no credit card, which is enough to test whether the output quality fits your documents before you build any automation around it. When you are ready to scale a batch pipeline, our API integration guide shows how to drive conversions from your own code. If you prefer to stay in one language end to end, the Python conversion guide walks through building batch pipelines from scratch.

A common hybrid pattern works well: use a fast local tool for the easy, text-based documents, and route the hard cases (scans, dense tables, unusual layouts) to a hosted converter that handles them reliably. You get speed where you can and quality where you need it.

A Complete End-to-End Example

Here is a full script that ties the ideas together. It walks a folder recursively, skips files already converted, logs the outcome of each file, and prints a summary at the end:

#!/usr/bin/env bash
set -euo pipefail

SRC="${1:-./pdfs}"
DEST="${2:-./markdown}"
mkdir -p "$DEST"

ok=0
fail=0

find "$SRC" -type f -name "*.pdf" -print0 | while IFS= read -r -d '' f; do
  base="$(basename "${f%.pdf}")"
  out="$DEST/$base.md"
  [ -f "$out" ] && { echo "skip $base"; continue; }

  if pdftotext -layout "$f" - | pandoc -t gfm -o "$out" 2>> "$DEST/errors.log"; then
    echo "ok   $base"; ok=$((ok+1))
  else
    echo "fail $base"; fail=$((fail+1))
  fi
done

echo "Converted $ok files, $fail failed. See $DEST/errors.log for details."

Save this as batch-convert.sh, make it executable with chmod +x batch-convert.sh, and run it with an input and output folder. It is a solid starting point that you can extend with parallelism, OCR, or a call to a hosted API for the files that plain extraction cannot handle. To see how document conversion fits into broader workflows, browse the use cases for ideas.

Frequently Asked Questions

Can Pandoc convert PDF to Markdown directly?

Not on its own. Pandoc does not accept PDF as an input format because PDF describes visual layout rather than document structure. The standard workaround is to extract the text first with a tool like pdftotext and then pipe that into Pandoc. For clean, digital-native PDFs this works well. For scans or complex layouts, a structure-aware tool like Marker gives much better results.

What is the fastest way to convert hundreds of PDFs?

For clean text-based files, a parallelized Pandoc or pdftotext loop using xargs -P or GNU parallel is very fast because it uses every CPU core. For messy or scanned documents, throughput drops because the tools do more work per page, so batch those overnight or offload them to a hosted converter that handles the heavy lifting for you.

How do I handle filenames with spaces in a batch script?

Always quote your variables ("$f" rather than $f) and prefer find -print0 piped into while IFS= read -r -d ''. This treats each filename as a single unit even when it contains spaces, quotes, or other special characters, which is the most frequent cause of batch scripts breaking partway through a run.

Will batch conversion preserve tables and images?

It depends on the tool. Plain text extraction tends to flatten tables and drops images entirely. Structure-aware tools like Marker preserve tables as Markdown and can export embedded images to a folder. If fidelity matters, test on a few representative documents before running the full batch, and consider a hosted converter for the documents where accuracy is critical.

Can I automate this to run on a schedule?

Yes. Once your conversion is a script, you can trigger it with cron on Linux or macOS, or Task Scheduler on Windows, pointed at a watched folder. Because a well-written script skips files it has already converted, it is safe to run repeatedly. It will only pick up new files each time.

The Takeaway

Batch converting PDFs to Markdown from the command line comes down to one pattern: find the files, loop over them, convert each, and write the output. Pandoc is the fast choice for clean text documents, Marker is the accurate choice for complex or scanned files, and a portable shell loop lets you wrap any converter into a repeatable, schedulable job.

Start with the end-to-end script above, test it on a small folder, and add logging and parallelism as your needs grow. And when the maintenance of local tooling outweighs the benefit, or when the documents are simply too messy for extraction, a hosted converter handles the hard cases so you can focus on what you do with the Markdown, not on wrestling it out of the PDF.

Rafael Abellan

About the author

Rafael Abellan

Founder, PDFtoMD

Rafael Abellan is the founder of Agência Triva and ships independent side projects in parallel. PDFtoMD came out of a personal frustration: he kept burning through Claude AI's token limit by uploading long PDFs, then losing hours waiting for the cap to reset. He built the tool to fix his own workflow, and now uses it every day.

Ready to convert your PDFs to Markdown?

Free account · 3 conversions/month · No credit card required