Skip to content

Instantly share code, notes, and snippets.

@BrunoGrandePhD
Created June 19, 2016 23:53
Show Gist options
  • Save BrunoGrandePhD/30e611b57920602866b27d27d700a396 to your computer and use it in GitHub Desktop.
Save BrunoGrandePhD/30e611b57920602866b27d27d700a396 to your computer and use it in GitHub Desktop.
Sometimes, the font is messed up on a printed version of a PDF file. It seems that this may be caused by the missing embedded fonts in the PDF file. The following shell function simplifies the process.
# Ensure that fonts are embedded in a PDF file for correct printing
# Credit: http://stackoverflow.com/a/4234150
function fixpdf {
# Print usage is no arguments are given
if [ "$#" -eq 0 ]; then
echo "Usage: fixpdf PDF_FILE [PDF_FILE ...]"
fi
# Check if ghostscript is installed
if ! type gs >/dev/null 2>&1; then
echo 'Error: ghostscript is either not installed or not in your PATH.'
echo 'Run `brew install gs` to install ghostscript.'
return 1
fi
# Iterate over given PDF files
for file in "$@"; do
dir=$(dirname "$file")
base=$(basename "$file" .pdf)
gs \
-sFONTPATH=/Library/Fonts:${HOME}/Library/Fonts \
-o $dir/$base.fixed.pdf \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
"$file"
done
}
@BrunoGrandePhD
Copy link
Author

Note that -sFONTPATH is currently set to values that are tailored for OS X. Adjust accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment