Skip to content

Instantly share code, notes, and snippets.

@edouard-lopez
Last active November 3, 2025 00:29
Show Gist options
  • Select an option

  • Save edouard-lopez/e2d53cee78d0bfa75b6a5bacac363030 to your computer and use it in GitHub Desktop.

Select an option

Save edouard-lopez/e2d53cee78d0bfa75b6a5bacac363030 to your computer and use it in GitHub Desktop.
compress-pdf (90% compression with simple commands)

I was looking to compress PDF files (typically poster for event and such are massive)

  • online tools is not a solution for sensitive data, process is tiedous to check compression rate ;
  • other tools require Python (cf. minimalpdfcompress, pdfc) ; but Local compress of PDF with ~90% efficiency (way better than online tool) using only CLI tool
  • in-browser solution compress so much that content isn't readable (cf. CompressPDF) ;
  • DjVu format is super efficient as it's designed to store document
  • Currently no OCR, if you need it check pdf2djvu-ocr

Requirement

sudo apt-get install \
    djvulibre-bin \
    ghostscript \
    pdf2djvu

Usage

compress-pdf input-big.pdf [compressed.pdf]
compress_pdf() {
local inputFile="$1"
local outputFile="$2"
local djvuFile
if [ -z "$inputFile" ]; then
echo "Usage: compress_pdf inputFile [outputFile]"
return 1
fi
if [ -z "$outputFile" ]; then
outputFile="${inputFile%.pdf}.compressed.pdf"
fi
djvuFile="${inputFile%.pdf}.djvu"
if pdf2djvu --dpi=300 --output="$djvuFile" "$inputFile"; then
echo "✅ Converted to Djvu"
else
echo "❌ Failed to convert to Djvu"
return 1
fi
if ddjvu -format=pdf -quality=85 "$djvuFile" "$outputFile"; then
echo "✅ Compressed back to PDF"
else
echo "❌ Failed to compress to PDF"
return 1
fi
}
function compress-pdf \
--description 'Compress a PDF file by converting it to Djvu and back to PDF. outputFile name is optional' \
--argument-names inputFile \
--argument-names outputFile \
--argument-names dpi \
--argument-names quality
if not test -f "$inputFile"
echo "❌ Input file not found: $inputFile"
return 1
end
set djvuFile (string replace '.pdf' '.djvu' $inputFile)
set --query dpi[1]; or set dpi 300 # smaller dpi means smaller file size but lower quality
pdf2djvu --dpi=$dpi --output="$djvuFile" "$inputFile" --verbose
if test $status -eq 0
echo "✅ Converted to Djvu"
else
echo "❌ Failed to convert to Djvu"
return 1
end
set --query outputFile[1]; or set outputFile (string replace '.pdf' '.compressed.pdf' $inputFile)
set --query quality[1]; or set quality 75 # smaller quality means smaller file size but lower quality
ddjvu -verbose -format=pdf -quality=$quality "$djvuFile" "$outputFile"
if test $status -eq 0
echo "✅ Compressed back to PDF"
else
echo "❌ Failed to compress to PDF"
return 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment