Compresspdf.sh: Difference between revisions
No edit summary Tag: Reverted |
No edit summary |
||
| (9 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
==Script== | ==Script== | ||
[[File:compresspdf-2.png|right|A PDF's size before compression]] | [[File:compresspdf-2.png|thumb|right|A PDF's size before compression]] | ||
[[File:compresspdf-1.png|right|The resampled PDF's size, after compression]] | [[File:compresspdf-1.png|thumb|right|The resampled PDF's size, after compression]] | ||
<pre> | <pre> | ||
| Line 46: | Line 46: | ||
After installing the software <code>ghostscript</code> (see above), save the code above as a bash script with the <code>.sh</code> extension, for example: <code>compresspdf.sh</code>. Don't forget to add <code>#! /bin/bash</code> to the top of your script! | After installing the software <code>ghostscript</code> (see above), save the code above as a bash script with the <code>.sh</code> extension, for example: <code>compresspdf.sh</code>. Don't forget to add <code>#! /bin/bash</code> to the top of your script! | ||
Then, in a terminal session | Make the script executable with | ||
<code>$ chmod a+x compresspdf.sh</code> | |||
Then, in a terminal session add the PDF you want to compress to the same directory as where <code>compresspdf.sh</code> is saved. | |||
Then do: | Then do: | ||
<code>./compresspdf.sh <file.pdf> <resolution in DPI></code> | <code>$ ./compresspdf.sh <file.pdf> <resolution in DPI></code> | ||
For example: | For example: | ||
<code>./compresspdf.sh input.pdf 300</code> | <code>$ ./compresspdf.sh input.pdf 300</code> | ||
The output will be something like: | The output will be something like: | ||
| Line 62: | Line 66: | ||
==Making a portfolio PDF at the last minute== | ==Making a portfolio PDF at the last minute== | ||
This script has been used many times on many machines. It's super handy when making files that need to be sent over a network. One particular use was when making a final final final portfolio PDF for a grant application at the eleventh hour. | This script has been used many times on many machines by many users. It's super handy when making files that need to be sent quickly over a network. One particular use was when making a final final final portfolio PDF for a grant application at the eleventh hour. | ||
Usually, the portfolio is the last thing I make. I | Usually, the portfolio is the last thing I make after I write the application text. I should use a template to work from, but I almost always make a file from scratch. I should be better organised! I add images willy-nilly, tailoring them to the particular grant application. And using CSS-to-print to make a PDF means that your software doesn't have a handy option to compress files when exporting, you have to add this in to the workflow manually. | ||
On the final day of application, January 31st, the resulting PDF was quite large (122,5mb!). I needed to send this through a webform, and the specification was that it could not be bigger than 10mb. | |||
So, I used this script | So, I used this script just several hours before submitting the PDF. I managed to get the file down to a manageable size without stress, and pressed send in time. In the end I did not get the grant, but I got the script :) | ||
[[Category: | [[Category:Boilerplates]] | ||
Latest revision as of 17:08, 4 February 2026
Script


#! /bin/bash
pdffile=$1;
dpi=$2
gs \
-o "${pdffile%.pdf}-resampled.pdf" \
-sDEVICE=pdfwrite \
-dDownsampleColorImages=true \
-dDownsampleGrayImages=true \
-dDownsampleMonoImages=true \
-dColorImageResolution=$dpi \
-dGrayImageResolution=$dpi \
-dMonoImageResolution=$dpi \
-dColorImageDownsampleThreshold=1.0 \
-dGrayImageDownsampleThreshold=1.0 \
-dMonoImageDownsampleThreshold=1.0 \
"${pdffile}"
How to work with it?
What does this script do?
This script squashes large PDFs into very small ones.
In which context was it made?
The script is written by Open Source Publishing. It has since travelled through other practices to be used for a variety of applications, usually at the eleventh hour before sending PDFs via email and webforms.
What software does it use?
ghostscript
How to download these pieces of software?
Go to the URL https://www.ghostscript.com/, you can download it from there.
On which systems can this script run?
This works from the command line in terminal sessions on Linux systems, also on Mac OS. Windows has a different shell, and it is likely it won't work there.
How to run the script?
After installing the software ghostscript (see above), save the code above as a bash script with the .sh extension, for example: compresspdf.sh. Don't forget to add #! /bin/bash to the top of your script!
Make the script executable with
$ chmod a+x compresspdf.sh
Then, in a terminal session add the PDF you want to compress to the same directory as where compresspdf.sh is saved.
Then do:
$ ./compresspdf.sh <file.pdf> <resolution in DPI>
For example:
$ ./compresspdf.sh input.pdf 300
The output will be something like:
input-resampled.pdf
Making a portfolio PDF at the last minute
This script has been used many times on many machines by many users. It's super handy when making files that need to be sent quickly over a network. One particular use was when making a final final final portfolio PDF for a grant application at the eleventh hour.
Usually, the portfolio is the last thing I make after I write the application text. I should use a template to work from, but I almost always make a file from scratch. I should be better organised! I add images willy-nilly, tailoring them to the particular grant application. And using CSS-to-print to make a PDF means that your software doesn't have a handy option to compress files when exporting, you have to add this in to the workflow manually.
On the final day of application, January 31st, the resulting PDF was quite large (122,5mb!). I needed to send this through a webform, and the specification was that it could not be bigger than 10mb.
So, I used this script just several hours before submitting the PDF. I managed to get the file down to a manageable size without stress, and pressed send in time. In the end I did not get the grant, but I got the script :)