From 1b77cdd3832585d91848e90fc79f340e44787989 Mon Sep 17 00:00:00 2001 From: curben <2809763-curben@users.noreply.gitlab.com> Date: Thu, 23 Jan 2020 21:21:33 +0000 Subject: [PATCH] post(pdf-compress): rename original file --- source/_posts/make-pdf-files-smaller.md | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/source/_posts/make-pdf-files-smaller.md b/source/_posts/make-pdf-files-smaller.md index 231787e..abfc36d 100644 --- a/source/_posts/make-pdf-files-smaller.md +++ b/source/_posts/make-pdf-files-smaller.md @@ -53,10 +53,14 @@ Use the following script to compress all PDFs in a folder. Usage: `sh pdfcompress.sh 'target folder'` +*If `'target folder'` is not specified, defaults to current directory.* + ```bash #!/bin/sh -cd "$1" +if [ -n "$1" ]; then + cd "$1" +fi for i in *.pdf; do [ -f "$i" ] || break @@ -64,8 +68,7 @@ for i in *.pdf; do # Skip compressed PDFs echo "$i" | grep --quiet ".compressed.pdf" - if [ $? = 1 ] - then + if [ $? = 1 ]; then gs \ -sOutputFile="${i%.*}.compressed.pdf" \ -sDEVICE=pdfwrite \ @@ -80,5 +83,36 @@ for i in *.pdf; do done ``` +If you prefer to use the original filename as the compressed version and rename the original uncompressed's to "*.original.pdf", + +*Following script doesn't skip previously compressed PDFs* + +```bash +#!/bin/sh + +if [ -n "$1" ]; then + cd "$1" +fi + +for i in *.pdf; do + [ -f "$i" ] || break + + # Rename original file to *.original.pdf + original="${i%.*}.original.pdf" + mv "$i" "$original" + + gs \ + -sOutputFile="$i" \ + -sDEVICE=pdfwrite \ + -dPDFSETTINGS=/ebook \ + -sColorConversionStrategy=Gray \ + -sColorConversionStrategyForImages=/Gray \ + -dProcessColorModel=/DeviceGray \ + -dCompatibilityLevel=1.4 \ + -dNOPAUSE -dBATCH -dQUIET \ + "$original" +done +``` +

Source: [Internal Pointers](https://www.internalpointers.com/post/compress-pdf-file-ghostscript-linux), [firstdoit](https://gist.github.com/firstdoit/6390547), [ahmed-musallam](https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9), [Ghostscript Docs](https://ghostscript.com/doc/current/Ps2pdf.htm)