Compressing PDFs with a Simple CLI Command
Have you ever needed to compress a PDF but didn’t want to use bulky software or online tools? Today, I learned about a neat way to do it using Ghostscript, a powerful command-line tool.
Why Use This?
Here’s the command in action:
bashcompresspdf() {gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${3:-"screen"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"}
This little shell function, named compresspdf, takes three arguments:
How It Works
The function uses Ghostscript (gs), a versatile tool for processing PDFs and PostScript files, with the following options:
Create a bash script file compresspdf.sh with bellow content
bash#!/bin/bash# Usage: compresspdf [input file] [output file] [screen*|ebook|printer|prepress]# Check that the required arguments are providedif [ $# -lt 2 ]; thenecho "Usage: compresspdf [input file] [output file] [screen*|ebook|printer|prepress]"exit 1ficompresspdf() {gs -sDEVICE=pdfwrite -dNOPAUSE -dQUIET -dBATCH -dPDFSETTINGS=/${3:-"ebook"} -dCompatibilityLevel=1.4 -sOutputFile="$2" "$1"}compresspdf "$1" "$2" "$3"
Make it executable
bashchmod +x ./compresspdf.sh
Usage
Here’s how to use the function:
Run it with the required arguments:
bash./compresspdf.sh input.pdf output.pdf [compression_level]
For example:
bash./compresspdf.sh ./input.pdf ./output.pdf ebook
If you don’t specify a compression level, it defaults to screen, which is optimized for smaller file sizes.
This function is a handy addition to any developer’s toolkit, saving time and effort while maintaining full control over PDF compression.
Do you use a similar tool or script for handling PDFs? Let me know in the comments!
Table of Contents
- How to trigger a lambda function only if multiple s3 events are met
[TIL] Google Oauth2 with ReactJS x Django - The easy way
Learn how to implement Google OAuth2 authentication with a Django backend and ReactJS frontend. This comprehensive guide walks you through setting up Google API credentials, handling user login and consent, and retrieving user data from Google. Follow detailed steps for integrating Google login using @react-oauth/google in ReactJS and creating secure backend APIs with Django to manage JWT tokens and user information. Perfect for developers looking to integrate Google authentication into their web applications, this tutorial includes practical code examples and best practices for seamless user authentication.