# PDF Document hack with MacOS Terminal only
###### tags: `compress pdf for free`, `convert pdf to jpg`, `make pdf looks scanned`, `make pdf look like scan`
### 1. Convert pdf to small size
Install ghostscript from aptitude.
```
brew install ghostscript
```
Now, you can use ghostscript to compress your PDF file for the web:
```
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -sOutputFile=output.pdf input.pdf
```
Type: [screen|ebook|printer|prepress]
#### + additional enhancement
That’s not exactly a memorable command, so I’ve made a function that you can add to your ~/.bash_profile to allow you to easily compress PDF files from your terminal:
```
# Usage: compresspdf [input file] [output file] [screen*|ebook|printer|prepress]
compresspdf() {
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${3:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"
}
```
Now you can simply run the following command:
```
compresspdf "Massive.pdf" "Small.pdf"
```
Using this command I managed to compress an A3 size PDF (originally around 9MB) down to just under 1MB, which is perfect for the web.
[Source](https://blog.omgmog.net/post/compressing-pdf-from-your-mac-or-linux-terminal-with-ghostscript/)
### 2. Convert pdf to jpg
Prerequisites
```
brew install imagemagick
brew install ghostscript
```
```
magick -density 150 input.pdf -quality 80 output.jpg
```
Change the **density** up to 300 or **quality** up to 100 to adjust the file size and quality you like
[Source](https://imagemagick.org/script/download.php)
### 3. Make pdf looks like scanned
Prerequisites
```
brew install imagemagick poppler
```
Run this automotor
- Download the automotor script from this github
https://github.com/Robihamanto/make-pdf-look-like-scanned
- Follow the instruction on ReadMe.md file to run the automotor on macOS.
[Source](https://github.com/Coderwelsch/make-pdf-look-like-scanned?tab=readme-ov-file)
Script behind
```
#!/bin/bash
set -e
export PATH=/usr/local/bin:$PATH
export PATH=/opt/homebrew/bin:$PATH
for file in "$@"; do
base=${file%.pdf}
base=$base"_scanned.pdf"
# Split PDF into pages
echo "Splitting $base into separate pages"
pdfseparate "$file" /tmp/fake-scan-split-%04d.pdf
# Loop over the pages
for splitFile in /tmp/fake-scan-split-*.pdf; do
splitFileBase=${splitFile%.pdf}
splitFileScanned=$splitFileBase"_scanned.pdf"
# Slightly rotate page, add a bit of noise and output a flat pdf
convert -density 130 -trim -flatten "$splitFile" -attenuate 0.2 +noise Gaussian -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 8 + 1))" \( +clone -background black -shadow 30x5+5+5 \) +swap -background white -layers merge +repage "$splitFileScanned"
echo "Output page $splitFileBase to $splitFileScanned"
done
# Combine the PDFs, add noise across the entire document, apply sharpening, convert to b&w, soften the blacks slightly
convert -density 130 $(ls -rt /tmp/fake-scan-split-*_scanned.pdf) -attenuate 0.2 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray +level 15%,100% "$base"
echo "PDF re-combined to $base"
# Remove all the temporary PDFs
echo "Cleaning up"
rm /tmp/fake-scan-split-*.pdf
done
```