some fixes

main
Antonio De Lucreziis 2 years ago
parent 5f496df275
commit 63a8436f11

@ -5,12 +5,11 @@ set -euo pipefail
help_message="$(cat <<END help_message="$(cat <<END
usage: $(basename "$0") [OPTIONS...] INPUT_PDF OUTPUT_PDF usage: $(basename "$0") [OPTIONS...] INPUT_PDF OUTPUT_PDF
Converts a PDF file to a series of JPEG images using convert to reduce the quality Converts a PDF file to a series of JPEG images to reduce the quality
and then merges back the result in a single pdf and then merges them back in a single pdf.
Options: Options:
-q, --quality Set the JPEG quality (default: 75) -q, --quality Set the JPEG quality (default: 75)
-v, --verbose Enable verbose output, also shows an estimate of remaining time
-h, --help Display this help message -h, --help Display this help message
END END
@ -22,7 +21,6 @@ function show_usage() {
} }
quality=75 quality=75
verbose=false
while [[ $# -gt 0 ]] while [[ $# -gt 0 ]]
do do
@ -34,10 +32,6 @@ do
shift # past argument shift # past argument
shift # past value shift # past value
;; ;;
-v|--verbose)
verbose=true
shift # past argument
;;
-h|--help) -h|--help)
show_usage show_usage
;; ;;
@ -61,21 +55,19 @@ output_file="$2"
temp_pages_dir="$(mktemp -d)" temp_pages_dir="$(mktemp -d)"
function logf { function logf {
if [ "$verbose" = true ]; then printf "$@"
printf "$@"
fi
} }
logf "Rendering each page to a jpeg image (quality = $quality%)\n" logf "Rendering each page to a jpeg image (quality = $quality)\n"
pdftoppm -r 300 -jpeg -jpegopt "quality=$quality,optimize=y" -progress $input_file "$temp_pages_dir/page" pdftoppm -r 300 -jpeg -jpegopt "quality=$quality,optimize=y" -progress $input_file "$temp_pages_dir/page"
for f in *.jpg; do for f in "$temp_pages_dir"/page-*.jpg; do
log "Wrapping $f into ${f/.jpg/.pdf}\n" logf "Wrapping $f into ${f/.jpg/.pdf}\n"
gs -dNOSAFER -sDEVICE=pdfwrite -o "${f/.jpg/.pdf}" /usr/share/ghostscript/lib/viewjpeg.ps -c "($f)" viewJPEG gs -dNOSAFER -sDEVICE=pdfwrite -o "${f/.jpg/.pdf}" /usr/share/ghostscript/lib/viewjpeg.ps -c "($f)" viewJPEG
done done
logf "Merging compressed pages to a single PDF ()\n" logf "Merging compressed pages to a single PDF ()\n"
pdfunite "$temp_pages_dir"/page*.pdf $output_file pdfunite "$temp_pages_dir"/page-*.pdf $output_file
rm -r "$temp_pages_dir" rm -r "$temp_pages_dir"
logf "Done\n" logf "Done\n"

Loading…
Cancel
Save