Skip to content

Instantly share code, notes, and snippets.

@ohlrogge
Last active December 22, 2024 20:15
Show Gist options
  • Save ohlrogge/572f7aa39c2346fe9a82449f4f715b5b to your computer and use it in GitHub Desktop.
Save ohlrogge/572f7aa39c2346fe9a82449f4f715b5b to your computer and use it in GitHub Desktop.
Batch Convert PDF to JPG and SVGs
#!/bin/bash
# This script requires ghostscript and pdf2svg to be installed
# Use brew to install ghostscript and pdf2svg on macOS
# brew install ghostscript pdf2svg
# In the terminal, navigate to the directory containing the PDFs you want to convert
# Make the script executable by running 'chmod +x batch-convert-pdf-to-jpg-and-svg.sh'
# Run the script by running './batch-convert-pdf-to-jpg-and-svg.sh'
# Create directories for output files
jpgDir="images/jpgs"
svgDir="images/svgs"
mkdir -p $jpgDir $svgDir
# Convert PDFs to JPG
for pdf in *.pdf; do
gs -dNOPAUSE -dBATCH -sDEVICE=jpeg -r300 -sOutputFile="$jpgDir/${pdf%.pdf}-page-%03d.jpg" "$pdf"
done
# Convert PDFs to SVG
for pdf in *.pdf; do
baseName="${pdf%.pdf}"
pageCount=$(pdfinfo "$pdf" | grep Pages | awk '{print $2}')
for ((i=1; i<=pageCount; i++)); do
echo "Converting page $i of $pageCount in $pdf to SVG"
pdf2svg "$pdf" "$svgDir/${baseName}-page-$(printf "%03d" $i).svg" $i
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment