A CLI Tool to Simplify Compressing PDF Files with Ghostscript
MAY 15th, 2018 – v1.1.2 Released – Butterfly – Hotfix 2
- Did not bump version number in version check function of v1.1.1
- Added version number in header comment of code
MAY 14th, 2018 – v1.1.1 Released – Butterfly – Hotfix
- Did not include command building step for situations with no WARNINGS.
MAY 14th, 2018 – v1.1 Released – Butterfly
- Bolded and colored formating of output text to make it more readable
- Added new lines bewteen each output to make it more readable
- Added Error Handling (WARNING): Output file does not end with .pdf, verify with user that’s really what they want
- Added Error Handling (WARNING): Output file name matches a file in the output directory
- Added Error Handling (ERROR): Unsafe input & output file names
MAY 11th, 2018 – v1.0 Released – Birth
- The birth of cPDF!
Check file size of test.pdf:
[email protected]:~/test$ ll total 31M drwxrwxr-x 2 hkdb hkdb 4.0K May 1 14:59 . drwxr-xr-x 94 hkdb hkdb 4.0K Apr 30 21:45 .. -rw-rw-r-- 1 hkdb hkdb 31M Apr 29 23:32 test.pdf
[email protected]:~/test$ cpdf help This is a script to compress a pdf file with the convention of: cpdf [type: screen, ebook, printer, prepress, or default] [input file name] [output file name] . To learn more about the different types, use the "types" flag (ie. cpdf types)
List the preset types of compression settings:
[email protected]:~/test$ cpdf types screen - selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting. ebook - selects medium-resolution output similar to the Acrobat Distiller "eBook" setting. printer - selects output similar to the Acrobat Distiller "Print Optimized" setting. prepress - selects output similar to Acrobat Distiller "Prepress Optimized" setting. default - selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
[email protected]:~/test$ cpdf ebook test.pdf compressed.pdf [email protected]:~/test$ ll total 35M drwxrwxr-x 2 hkdb hkdb 4.0K May 1 15:00 . drwxr-xr-x 94 hkdb hkdb 4.0K Apr 30 21:45 .. -rw-rw-r-- 1 hkdb hkdb 4.0M May 1 15:02 compressed.pdf -rw-rw-r-- 1 hkdb hkdb 31M Apr 29 23:32 test.pdf [email protected]:~/test$
Notice that compressed.pdf is 4M; the results from compressing a 31M pdf? I then opened up compressed.pdf and it still looked great!
[email protected]:~/test$ cpdf version cPDF v1.1 [email protected]:~/test$
As of v1.1, color coding certain output text has been added to improve readability:
Under the Hood
It essentially takes your arguments and turns it into the following Ghostscript command:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.6 -dPDFSETTINGS=/ebook
-dNOPAUSE -dQUIET -dBATCH -sOutputFile=[compressed.pdf]
Currently, if any of the below conditions are met, the script will print an error/warning message and exit. This is designed to prevent any dangerous execution of Ghostscript and to remind users in case they forget to input the command with the right convention. For the ones that are questionable, it will warn the user and provide a chance to cancel or confirm.
– Less than 3 arguments given
– Invalid compression type argument
– Input File Does Not End with .pdf
– Input File and Output File are the same
– Input File Name Contains Unsupported Characters(/\\:;\`)
– Output File Name Contains Unsupported Characters(/\\:;\`)
Questionable Conditions that the application will verify with User via A Dialog Message:
– Output File does not End with .pdf, Verify with User That’s Really What They Want
– Output File Name Matches a File in the Output Directory
To make this script behave more like a command, move it to a bin directory of choice that’s defined in your env.
This way, you can be in any directory and run this script to compress any pdf file without specifying the full path.
Many thanks to Dr. Haggen So for sharing the following link that inspired me to write this script:
Want a graphical alternative instead?
Check out Densify, a GTK+ GUI Application written in Python that simplifies compressing PDF files with Ghostscript