added a better version of pdfcompress that doesn't use convert

main
Antonio De Lucreziis 2 years ago
parent 1fac23e09a
commit 5f496df275

@ -6,6 +6,10 @@
With the verbose option this will also print processing information and the estimated remaining time.
- [`pdfcompress2`](./bin/pdfcompress2)
Variant of the previous command that uses `poppler` utils and `gs` instead of convert that is old and buggy (this was acually manually modified)
- [`hash2addr`](./bin/hash2addr)
Outputs a randomly generated IP address and port number based on the MD5 hash of the input string.

@ -76,7 +76,10 @@ start=$(date +%s.%N)
for i in $(seq 0 $((page_count-1))); do
iter_start=$(date +%s.%N)
convert -density 300 -quality "$quality" "${input_file}[$i]" "$temp_pages_dir/$i.jpg"
# convert -density 300 -quality "$quality" "${input_file}[$i]" "$temp_pages_dir/$i.jpg"
# Equivalent "gs" command that avoids convert's security policies
gs -sDEVICE=jpeg -o "$temp_pages_dir/$i.jpg" -dFirstPage="$i" -dLastPage="$i" -dJPEGQ="$quality" -r300x300 "$input_file"
# Compute estimated remaining time
end=$(date +%s.%N)

@ -0,0 +1,81 @@
#!/bin/bash
set -euo pipefail
help_message="$(cat <<END
usage: $(basename "$0") [OPTIONS...] INPUT_PDF OUTPUT_PDF
Converts a PDF file to a series of JPEG images using convert to reduce the quality
and then merges back the result in a single pdf
Options:
-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
END
)"
function show_usage() {
echo "$help_message"
exit 1
}
quality=75
verbose=false
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-q|--quality)
quality="$2"
shift # past argument
shift # past value
;;
-v|--verbose)
verbose=true
shift # past argument
;;
-h|--help)
show_usage
;;
-*)
echo "Unknown option: $key"
show_usage
;;
*)
break
;;
esac
done
if [ -z "$1" ] || [ -z "$2" ]; then
show_usage
fi
input_file="$1"
output_file="$2"
temp_pages_dir="$(mktemp -d)"
function logf {
if [ "$verbose" = true ]; then
printf "$@"
fi
}
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"
for f in *.jpg; do
log "Wrapping $f into ${f/.jpg/.pdf}\n"
gs -dNOSAFER -sDEVICE=pdfwrite -o "${f/.jpg/.pdf}" /usr/share/ghostscript/lib/viewjpeg.ps -c "($f)" viewJPEG
done
logf "Merging compressed pages to a single PDF ()\n"
pdfunite "$temp_pages_dir"/page*.pdf $output_file
rm -r "$temp_pages_dir"
logf "Done\n"
Loading…
Cancel
Save