From 37d1745041e73be7fb0e5b8ec3d4bd6bed41f04a Mon Sep 17 00:00:00 2001 From: Antonio De Lucreziis Date: Thu, 16 Feb 2023 02:06:30 +0100 Subject: [PATCH] initial commit --- .gitignore | 11 +++++++ README.md | 5 +++ pdf-compress | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 pdf-compress diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3bcfa3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.env +*.local* + +node_modules/ + +bin/ +.out/ +out/ +dist/ + +.vscode/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..94315e3 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Scripts written with the help of ChatGPT + +- [`pdf-compress`](./pdf-compress) + + A utility that uses convert to render each page of a PDF to a JPEG image and then merges them back into a single document. \ No newline at end of file diff --git a/pdf-compress b/pdf-compress new file mode 100755 index 0000000..d63e5f7 --- /dev/null +++ b/pdf-compress @@ -0,0 +1,92 @@ +#!/bin/bash + +set -euo pipefail + +function show_usage { + cat <<-END + usage: $0 [OPTIONS...] input_file output_file + + 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 + -h, --help Display this help message +END + 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 "Converting \"$input_file\" to \"$output_file\"\n" + +page_count=$(pdfinfo "$input_file" | grep Pages | awk '{print $2}') + +logf "Processing $page_count pages\n" + +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" + + # Compute estimated remaining time + end=$(date +%s.%N) + elapsed=$(echo "$end - $start" | bc) + iter_elapsed=$(echo "$end - $iter_start" | bc) + remaining=$(echo "($elapsed / ($i + 1)) * ($page_count - $i)" | bc) + remaining_fmt=$(printf "%02d:%02d:%02d" $((remaining/3600)) $((remaining/60)) $((remaining%60))) + + logf "Compressed page $((i+1)) in $(printf '%.2f' "$iter_elapsed")s (estimated remaining time $remaining_fmt)\n" +done + +logf "Merging compressed pages to a single PDF\n" +convert "$temp_pages_dir"/*.jpg "$output_file" + +rm -r "$temp_pages_dir" + +logf "Done\n"