2019-01-05 09:43:54 +00:00
|
|
|
---
|
|
|
|
title: Make PDF files smaller
|
2019-08-09 04:25:10 +00:00
|
|
|
excerpt: Convert to grayscale and reduce the resolution.
|
2019-05-19 02:05:03 +00:00
|
|
|
date: 2019-01-05
|
2019-01-05 09:43:54 +00:00
|
|
|
tags:
|
2019-06-21 01:58:05 +00:00
|
|
|
- pdf
|
2019-01-05 09:43:54 +00:00
|
|
|
---
|
|
|
|
|
2019-06-06 11:22:43 +00:00
|
|
|
The most effective ways of reducing the file size of a PDF is by converting to grayscale and reduce the resolution.
|
|
|
|
|
2019-01-05 09:43:54 +00:00
|
|
|
Requirement: ghostscript (installed by default in Ubuntu)
|
|
|
|
|
2019-01-16 06:46:17 +00:00
|
|
|
## Single PDF
|
|
|
|
|
2019-01-05 09:43:54 +00:00
|
|
|
Save the following script as "pdfcompress.sh".
|
|
|
|
|
|
|
|
Usage: `sh pdfcompress.sh input.pdf`
|
|
|
|
|
|
|
|
The output compressed file is named as "input.compressed.pdf"
|
|
|
|
|
|
|
|
```bash
|
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
filename=$(basename "$1")
|
|
|
|
dir=$(dirname "$1")
|
|
|
|
|
|
|
|
gs \
|
2019-10-06 09:55:47 +00:00
|
|
|
-sOutputFile="$dir/${filename%.*}.compressed.pdf" \
|
|
|
|
-sDEVICE=pdfwrite \
|
|
|
|
-dPDFSETTINGS=/ebook \
|
|
|
|
-sColorConversionStrategy=Gray \
|
|
|
|
-sColorConversionStrategyForImages=/Gray \
|
|
|
|
-dProcessColorModel=/DeviceGray \
|
|
|
|
-dCompatibilityLevel=1.4 \
|
|
|
|
-dNOPAUSE -dBATCH -dQUIET \
|
|
|
|
"$1"
|
2019-01-05 09:43:54 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
Options ([more info](https://ghostscript.com/doc/current/Ps2pdf.htm#Options)):
|
|
|
|
|
|
|
|
- Remove **ColorConversionStrategy**, **ColorConversionStrategyForImages** and **ProcessColorModel** lines to retain colour.
|
|
|
|
- **PDFSETTINGS**:
|
2019-10-06 09:55:47 +00:00
|
|
|
- */default* selects output intended to be useful across a wide variety of uses. 72 DPI.
|
|
|
|
- */screen* selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting. 72 DPI.
|
|
|
|
- */ebook* selects medium-resolution output similar to the Acrobat Distiller "eBook" setting. 150 DPI.
|
|
|
|
- */printer* selects output similar to the Acrobat Distiller "Print Optimized" setting. 300 DPI.
|
|
|
|
- */prepress* selects output similar to Acrobat Distiller "Prepress Optimized" setting. 300 DPI.
|
2019-01-05 09:43:54 +00:00
|
|
|
|
2019-01-16 06:46:17 +00:00
|
|
|
## PDFs in a folder
|
|
|
|
|
2019-01-05 09:43:54 +00:00
|
|
|
Use the following script to compress all PDFs in a folder.
|
|
|
|
|
|
|
|
Usage: `sh pdfcompress.sh 'target folder'`
|
|
|
|
|
2020-01-23 21:21:33 +00:00
|
|
|
*If `'target folder'` is not specified, defaults to current directory.*
|
|
|
|
|
2019-01-05 09:43:54 +00:00
|
|
|
```bash
|
|
|
|
#!/bin/sh
|
|
|
|
|
2020-01-23 21:21:33 +00:00
|
|
|
if [ -n "$1" ]; then
|
|
|
|
cd "$1"
|
|
|
|
fi
|
2019-01-05 09:43:54 +00:00
|
|
|
|
|
|
|
for i in *.pdf; do
|
|
|
|
[ -f "$i" ] || break
|
|
|
|
|
2019-10-06 09:55:47 +00:00
|
|
|
# Skip compressed PDFs
|
|
|
|
echo "$i" | grep --quiet ".compressed.pdf"
|
|
|
|
|
2020-01-23 21:21:33 +00:00
|
|
|
if [ $? = 1 ]; then
|
2019-10-06 09:55:47 +00:00
|
|
|
gs \
|
|
|
|
-sOutputFile="${i%.*}.compressed.pdf" \
|
|
|
|
-sDEVICE=pdfwrite \
|
|
|
|
-dPDFSETTINGS=/ebook \
|
|
|
|
-sColorConversionStrategy=Gray \
|
|
|
|
-sColorConversionStrategyForImages=/Gray \
|
|
|
|
-dProcessColorModel=/DeviceGray \
|
|
|
|
-dCompatibilityLevel=1.4 \
|
|
|
|
-dNOPAUSE -dBATCH -dQUIET \
|
|
|
|
"$i"
|
|
|
|
fi
|
2019-01-05 09:43:54 +00:00
|
|
|
done
|
|
|
|
```
|
|
|
|
|
2020-01-23 21:21:33 +00:00
|
|
|
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
|
|
|
|
```
|
|
|
|
|
2019-06-11 09:45:57 +00:00
|
|
|
<br/><br/>
|
2019-01-05 09:43:54 +00:00
|
|
|
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)
|