post(pdf-compress): rename original file

This commit is contained in:
curben 2020-01-23 21:21:33 +00:00
parent d4fefb225f
commit 1b77cdd383
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
1 changed files with 37 additions and 3 deletions

View File

@ -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
```
<br/><br/>
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)