# Compressing PDF Files using MacOS or Linux

The method I’m using requires `ghostscript`. Ghostscript is a high quality, high performance Postscript and PDF interpreter and rendering engine.

If you’re using a Mac, you can install `ghostscript` using `brew`:

```
brew install ghostscript
```

If you’re using Linux you can install `ghostscript` from `aptitude`, or your package manager of choice.

Now, you can use `ghostscript` to compress your PDF file for the web:

{% code overflow="wrap" %}

```
gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/screen -dCompatibilityLevel=1.4 -sOutputFile=output.pdf input.pdf
```

{% endcode %}

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:

{% code overflow="wrap" %}

```
# 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"
}
```

{% endcode %}

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.smhuda.com/misc/scripts/compressing-pdf-files-using-macos-or-linux.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
