Skip to content

Instantly share code, notes, and snippets.

@simonesestito
Created October 12, 2023 11:20
Show Gist options
  • Save simonesestito/07f164e66e6a5a5ca2e724dc3bc5b834 to your computer and use it in GitHub Desktop.
Save simonesestito/07f164e66e6a5a5ca2e724dc3bc5b834 to your computer and use it in GitHub Desktop.
Rasterize PDF file on Linux (bash function)
#!/bin/bash
#
# Script to rasterize a PDF file
#
# You can adjust the density ("convert" argument)
# or other options as you wish
#
# It is a function so that you can include in your .bashrc (or whatever)
# and use as a command
#
#
# Based on: https://superuser.com/a/1588781
#
rasterizePdf() {(
set -e
if [ $# -eq 0 ]; then
echo "Rasterize a PDF" >&2
echo "Usage: $0 <pdf_file>" >&2
exit 1
fi
for input in "$@"; do
tmp="$(mktemp).pdf"
output="${input%.*}-raster.pdf"
convert \
-render \
-density 300 \
"$input" \
"$tmp"
gs \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dNOPAUSE \
-dQUIET \
-dBATCH \
-sOutputFile="$output" \
"$tmp"
rm -f "$tmp" "${tmp%.*}"
echo -e "✅ $input \033[2m\n to $output\033[0m"
done
)}
# rasterizePdf YOUR_FILE.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment