Posted on
Artificial Intelligence

Artificial Intelligence PDF Workflows

Author
  • User
    linuxbash
    Posts by this author
    Posts by this author

AI-Powered PDF Workflows in Bash: From OCR to Q&A, All on Linux

If you live in your terminal and drown in PDFs—scanned contracts, research papers, invoices—this one’s for you. With a few battle-tested CLI tools and a sprinkle of AI, you can turn any PDF pile into searchable, summarized, queryable knowledge. No vendor lock-in, no heavy GUIs—just Bash.

What you’ll get from this guide:

  • Why AI + CLI is the fastest path from PDF chaos to clarity

  • Practical pipelines you can copy/paste today

  • Local or cloud AI options

  • Install commands for apt, dnf, and zypper for every tool mentioned


Why AI PDF workflows are worth your time

  • PDFs are “final-form” documents—beautiful for printing, painful for data. AI helps recover structure, summaries, and answers.

  • Bash makes these pipelines reproducible, scriptable, and automatable across servers and laptops.

  • Local models (via Ollama) keep sensitive docs on your machine. Cloud models add top-tier reasoning when needed.

  • Composable CLI tools (pdftotext, ocrmypdf, qpdf, jq) handle the boring parts so AI can focus on high-value tasks.


Tools we’ll use

  • poppler tools: pdftotext, pdfimages, pdfinfo

  • OCR: ocrmypdf + tesseract

  • PDF utilities: qpdf, ghostscript

  • Images: ImageMagick

  • Glue: curl, jq, parallel (optional)

  • AI: Ollama (local) or a cloud API


Install everything (apt, dnf, zypper)

Base packages:

  • Ubuntu/Debian (apt):
sudo apt update
sudo apt install -y poppler-utils ocrmypdf tesseract-ocr imagemagick qpdf ghostscript jq curl parallel libimage-exiftool-perl
  • Fedora/RHEL (dnf):
sudo dnf install -y poppler-utils ocrmypdf tesseract ImageMagick qpdf ghostscript jq curl parallel perl-Image-ExifTool
  • openSUSE (zypper):
sudo zypper install -y poppler-tools ocrmypdf tesseract-ocr ImageMagick qpdf ghostscript jq curl gnu_parallel exiftool

Tesseract language packs (English; adjust for your language):

  • apt: sudo apt install -y tesseract-ocr-eng

  • dnf: sudo dnf install -y tesseract-langpack-eng

  • zypper: sudo zypper install -y tesseract-ocr-traineddata-english

Optional Python (for scripting glue if needed):

  • apt: sudo apt install -y python3 python3-pip

  • dnf: sudo dnf install -y python3 python3-pip

  • zypper: sudo zypper install -y python3 python3-pip

Local AI (Ollama):

curl -fsSL https://ollama.com/install.sh | sh
# Then pull a model:
ollama pull llama3
# For image understanding later:
ollama pull llava

Cloud AI (example: OpenAI):

# Requires an API key:
export OPENAI_API_KEY="sk-..."

1) OCR and normalize any PDF

Make scanned PDFs searchable, then produce clean text for AI.

# 1) OCR scanned.pdf -> clean, searchable PDF
ocrmypdf --language eng --optimize 3 --deskew --clean \
  scanned.pdf ocr.pdf

# 2) (optional) Linearize and fix structure
qpdf --linearize ocr.pdf fixed.pdf

# 3) (optional) Compress while preserving text
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH \
  -sOutputFile=small.pdf fixed.pdf

# 4) Extract text (layout tries to keep columns)
pdftotext -layout -enc UTF-8 small.pdf text.txt

# 5) Quick metadata check
pdfinfo small.pdf
exiftool small.pdf

Why it matters:

  • ocrmypdf adds a hidden text layer to images, enabling search and copy/paste.

  • qpdf and ghostscript fix/compact messy PDFs that trip up AI.

  • pdftotext gives you a reliable text stream you can chunk and prompt.


2) Summarize and ask questions—locally or via API

Option A: Local with Ollama (private, offline-ready)

# Summarize a long PDF in chunks using a local LLM
FILE="small.pdf"
TMPTXT="$(mktemp)"
pdftotext -layout -enc UTF-8 "$FILE" "$TMPTXT"

# Chunk by ~8000 characters to control prompt size
cs=8000
nl -ba -w1 -s' ' "$TMPTXT" | fold -s -w $cs > chunks.txt

echo "# Summary for $FILE" > summary.md
i=0
while IFS= read -r chunk; do
  i=$((i+1))
  resp="$(printf 'Summarize this document chunk in 5 bullets with key facts and numbers:\n\n%s' "$chunk" \
    | ollama run llama3)"
  printf '\n## Chunk %d\n%s\n' "$i" "$resp" >> summary.md
done < chunks.txt

# Merge into a condensed final summary
final="$(printf 'Condense the following chunked summaries into a single executive summary with sections:\n\n%s' "$(cat summary.md)" | ollama run llama3)"
printf '\n# Final Executive Summary\n%s\n' "$final" >> summary.md
echo "Wrote summary.md"

Option B: Cloud via curl (fast, strong reasoning; watch costs)

# Simple Q&A with a PDF's text using OpenAI via curl/jq
export OPENAI_API_KEY="sk-..."
FILE="small.pdf"
TXT="$(pdftotext -layout -enc UTF-8 "$FILE" - | head -c 120000)" # trim for demo

curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model":"gpt-4o-mini",
    "messages":[
      {"role":"system","content":"You are a concise analyst."},
      {"role":"user","content":"Summarize key findings and action items:\n\n'"$(printf "%s" "$TXT" | jq -Rs . | sed 's/^"//;s/"$//' )"'"}
    ],
    "temperature":0.2
  }' | jq -r '.choices[0].message.content'

Tips:

  • Keep prompts explicit: “extract KPIs as JSON,” “find deadlines and owners,” etc.

  • For very long documents, use batching + a final “merge” prompt as above.


3) Extract images and tables, then use AI to structure them

Images: pull figures, charts, and diagrams, then describe with a vision model.

# Extract images
mkdir -p out_images
pdfimages -png small.pdf out_images/img

# Describe each image with a local vision model (llava)
for img in out_images/*.png; do
  echo "Image: $img"
  printf 'Describe this figure briefly. List any axes, units, and notable trends.' \
    | ollama run llava --image "$img"
  echo
done

Tables: quick-and-dirty table capture using layout text + AI to format as CSV.

# Extract a page range with likely tables and ask LLM for CSV
pdftotext -f 3 -l 6 -layout -enc UTF-8 small.pdf tables.txt

printf 'The following is a table-like text dump from a PDF. \
Reconstruct it into valid CSV with headers. \
Only output CSV, no commentary:\n\n%s' "$(cat tables.txt)" \
  | ollama run llama3 > tables.csv

echo "Wrote tables.csv"

Notes:

  • For complex tables, dedicated tools like Camelot/Tabula can help, but the above is often “good enough.”

  • Use -layout to preserve columns so the model can infer structure.


4) Process entire folders with GNU parallel

Turn the one-off pipeline into a repeatable batch job.

# ocr-and-summarize.sh
#!/usr/bin/env bash
set -euo pipefail
pdf="$1"
base="$(basename "${pdf%.*}")"
tmp="$(mktemp -d)"
outdir="out"; mkdir -p "$outdir"

# OCR if needed
if pdffonts "$pdf" 2>/dev/null | awk 'NR>2 {exit 1} END {exit 0}'; then
  # no fonts found -> likely image-only
  ocrmypdf --language eng --optimize 3 --deskew --clean "$pdf" "$tmp/ocr.pdf"
  src="$tmp/ocr.pdf"
else
  src="$pdf"
fi

# Normalize and extract text
qpdf --linearize "$src" "$tmp/fixed.pdf"
pdftotext -layout -enc UTF-8 "$tmp/fixed.pdf" "$tmp/text.txt"

# Summarize locally (swap with cloud if desired)
summary="$(printf 'Create a structured summary with sections: Overview, Key Findings, Risks, Actions.\n\n%s' "$(cat "$tmp/text.txt")" | ollama run llama3)"

printf '%s\n' "$summary" > "$outdir/$base.summary.md"
echo "OK: $pdf -> $outdir/$base.summary.md"

Run on a directory:

export OMP_NUM_THREADS=4
export OPENBLAS_NUM_THREADS=4
parallel -j 4 ./ocr-and-summarize.sh ::: *.pdf

5) Enrich with metadata and build citations via AI

Combine pdfinfo/exiftool + AI to generate standardized references.

FILE="small.pdf"
META="$(pdfinfo "$FILE")"
EXIF="$(exiftool -j "$FILE" | jq '.[0]')"

PROMPT=$(cat <<'EOF'
You are a citation assistant. Given PDF metadata and Exif fields,
produce a concise CSL-JSON entry. Fill missing fields sensibly and
avoid hallucinations—prefer "unknown" if not present.
Return ONLY valid JSON.
EOF
)

body=$(jq -n --arg m "$META" --arg e "$EXIF" --arg p "$PROMPT" '{
  model:"gpt-4o-mini",
  temperature:0,
  messages:[
    {role:"system",content:$p},
    {role:"user",content:("PDFINFO:\n"+$m+"\nEXIF:\n"+$e)}
  ]
}')

curl -s https://api.openai.com/v1/chat/completions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$body" | jq -r '.choices[0].message.content' > citation.json

jq . citation.json

Store citation.json alongside *.summary.md and you have a neat archive.


Real-world usage patterns

  • Daily report digests: drop PDFs into a folder, run parallel, ship summaries to Slack/Git.

  • Contract review: OCR > summarize risks/clauses > grep the output for red flags.

  • Research notes: extract figures, LLM captions, and CSV tables into a lab notebook repo.


Conclusion and next steps

PDFs don’t have to be a black box. With a small toolkit and either local or cloud AI, you can:

  • Make scans searchable

  • Summarize huge docs in minutes

  • Extract tables, charts, and metadata cleanly

  • Automate everything from your shell

Your next step: 1) Install the tools for your distro (commands above). 2) Run the OCR + summary pipeline on a sample PDF. 3) Expand to images/tables and batch with parallel.

Want more? Extend this into a RAG setup: chunk text, generate embeddings with ollama run nomic-embed-text, and do local semantic search over your PDF library. If you’d like a follow-up guide on that, tell me what stack you’re on and I’ll tailor it.