Last active
November 2, 2022 11:15
-
-
Save sepastian/2a89009e280f5fa982092d170a18b614 to your computer and use it in GitHub Desktop.
Compress PDF custom Oh-My-ZSH function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Place in $ZSH/.oh-my-zsh/custom. | |
# | |
# The following ZSH function uses GhostScript's `gs` command to reduce the file size of a PDF file. | |
# The result of compressing `test.pdf` will be written to `test.compressed.pdf`. | |
# If `test.compressed.pdf` exists, it will only be overwritten after confirmation. | |
# | |
# Now, restart/open a new shell and compress PDFs with: | |
# | |
# compress_pdf test.pdf | |
# File exists: test.compressed.pdf, overwrite (Y/n)? | |
# Wrote test.compressed.pdf. | |
# | |
# Based on https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9. | |
compress_pdf() { | |
f="${1}" | |
if [[ ! "${f}" =~ ^.*\.pdf ]]; then | |
echo "Usage: compress_pdf FILE.pdf" | |
return | |
fi | |
f2="${f/.pdf/.compressed.pdf}" | |
if [[ -f "${f2}" ]]; then | |
read "yn?File exists: ${f2}, overwrite (Y/n)? " | |
if [[ "$yn" =~ [Nn] ]]; then | |
echo "Not overwritting ${f2}, done." | |
return | |
fi | |
fi | |
gs \ | |
-q -dNOPAUSE -dBATCH -dSAFER \ | |
-sDEVICE=pdfwrite \ | |
-dCompatibilityLevel=1.3 \ | |
-dPDFSETTINGS=/screen \ | |
-dEmbedAllFonts=true -dSubsetFonts=true \ | |
-dColorImageDownsampleType=/Bicubic \ | |
-dColorImageResolution=144 \ | |
-dGrayImageDownsampleType=/Bicubic \ | |
-dGrayImageResolution=144 \ | |
-dMonoImageDownsampleType=/Bicubic \ | |
-dMonoImageResolution=144 \ | |
-sOutputFile="${f2}" \ | |
"${f}" | |
echo "Wrote ${f2}." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment