Created
March 8, 2021 16:09
-
-
Save paulodiovani/ce860e7b7a41b2cedbd57672d2494896 to your computer and use it in GitHub Desktop.
Compress/optimize PDF file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# AUTHOR: (c) Ricardo Ferreira | |
# NAME: Compress PDF 1.4 | |
# DESCRIPTION: A nice Nautilus script with a GUI to compress and optimize PDF files | |
# REQUIRES: ghostscript, poppler-utils, zenity | |
# LICENSE: GNU GPL v3 (http://www.gnu.org/licenses/gpl.html) | |
# WEBSITE: https://launchpad.net/compress-pdf | |
# Messages | |
# English (en-US) | |
error_nofiles="No file selected." | |
error_noquality="No optimization level selected." | |
error_ghostscript="PDF Compress requires the ghostscript package, which is not installed. Please install it and try again." | |
error_nopdf="The selected file is not a valid PDF archive." | |
label_filename="Save PDF as..." | |
label_level="Please choose an optimization level below." | |
optimization_level="Optimization Level" | |
level_default="Default" | |
level_screen="Screen-view only" | |
level_low="Low Quality" | |
level_high="High Quality" | |
level_color="High Quality (Color Preserving)" | |
job_done="has been successfully compressed" | |
VERSION="1.4" | |
ZENITY=$(which zenity) | |
pdf_file=$(basename "$1") | |
# Check if Ghostscript is installed | |
GS="/usr/bin/ghostscript" | |
if [ ! -x $GS ]; then | |
$ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_ghostscript" | |
exit 0; | |
fi | |
# Check if the user has selected any files | |
if [ -z "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" ]; then | |
$ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_nofiles" | |
exit 0; | |
fi | |
# Check if the selected file is a PDF | |
mimetype=$(file -b -i "$1") | |
if [ -z "`echo $mimetype | grep -i 'pdf' `" ]; then | |
$ZENITY --error --title="Compress PDF "$VERSION"" --text="$error_nopdf" | |
exit 0; | |
fi | |
# Choose output file name | |
temp_filename=.temp-"$pdf_file" | |
suggested_filename=compressed-"$pdf_file" | |
output_filename=$($ZENITY --file-selection --save --confirm-overwrite --filename="$suggested_filename" --title="$label_filename") | |
if [ "$?" = 1 ] ; then | |
exit 0; | |
fi | |
# Extract metadata from the original PDF | |
pdfinfo "$1" | sed -e 's/^ *//;s/ *$//;s/ \{1,\}/ /g' -e 's/^/ \//' -e '/CreationDate/,$d' -e 's/$/)/' -e 's/: / (/' > .pdfmarks | |
sed -i '1s/^ /[/' .pdfmarks | |
sed -i '/:)$/d' .pdfmarks | |
echo " /DOCINFO pdfmark" >> .pdfmarks | |
function gsoptimise { | |
local DPI=150 | |
local OUTPUT="$1" | |
shift | |
gs -q -dNOPAUSE -dBATCH -dSAFER \ | |
-sDEVICE=pdfwrite \ | |
-dCompatibilityLevel=1.4 \ | |
-dEmbedAllFonts=true \ | |
-dSubsetFonts=true \ | |
-dUseFlateCompression=true \ | |
-dOptimize=true \ | |
-dProcessColorModel=/DeviceRGB \ | |
-dUseCIEColor=true \ | |
-dDownsampleGrayImages=true \ | |
-dGrayImageDownsampleType=/Bicubic \ | |
-dGrayImageResolution=$DPI \ | |
-dAutoFilterGrayImages=false \ | |
-dDownsampleMonoImages=true \ | |
-dMonoImageDownsampleType=/Bicubic \ | |
-dMonoImageResolution=$DPI \ | |
-dDownsampleColorImages=true \ | |
-dColorImageDownsampleType=/Bicubic \ | |
-dColorImageResolution=$DPI \ | |
-dAutoFilterColorImages=false \ | |
-dPDFSETTINGS=/default \ | |
-sOutputFile="$OUTPUT" \ | |
"$@" | |
} | |
# Execute ghostscript while showing a progress bar | |
(echo "0" ; | |
gsoptimise "$temp_filename" "$1" .pdfmarks ; | |
rm .pdfmarks | |
echo "100") | (if `$ZENITY --progress --pulsate --auto-close --title="Compress PDF "$VERSION""`; | |
then | |
mv -f "$temp_filename" "$output_filename" & | |
notify-send "Compress PDF" "$pdf_file $job_done" | |
else | |
killall gs | |
rm "$temp_filename" | |
exit | |
fi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment