Created
May 23, 2022 02:34
-
-
Save capezotte/1bf25c00d9abab0d622d48d37c860316 to your computer and use it in GitHub Desktop.
ShrinkPDF (two step)
This file contains 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/sh | |
# Modified from https://github.com/aklomp/shrinkpdf | |
# Licensed under the 3-clause BSD license: | |
# | |
# Copyright (c) 2014-2022, Alfred Klomp | |
# Copyright (c) 2020-2022, Carlos E. | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# 1. Redistributions of source code must retain the above copyright notice, | |
# this list of conditions and the following disclaimer. | |
# 2. Redistributions in binary form must reproduce the above copyright notice, | |
# this list of conditions and the following disclaimer in the documentation | |
# and/or other materials provided with the distribution. | |
# 3. Neither the name of the copyright holder nor the names of its contributors | |
# may be used to endorse or promote products derived from this software | |
# without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
# POSSIBILITY OF SUCH DAMAGE. | |
set -e | |
cleanup() { | |
if [ -e "$OVERLAY" ]; then rm "$OVERLAY"; fi | |
if [ -e "$BACK" ]; then rm "$BACK"; fi | |
} | |
# Temporary files | |
OVERLAY="$(mktemp -t zpdf.XXXXXXXXXX)" | |
BACK="$(mktemp -t zpdf.XXXXXXXXXX)" | |
trap cleanup INT HUP TERM EXIT | |
# Output DPI | |
DPI=${3:-72} | |
INPUT=${1:?No input file.} | |
if [ -f "$INPUT" ]; then | |
echo "Input file: $INPUT" | |
else | |
>&2 printf 'No such file or directory: %s\n' "${INPUT}" | |
exit 1 | |
fi | |
OUT=${2:?No output file.} | |
printf 'Output file: %s\n' "$OUT" | |
# Wrapper with all relvant Ghostscript options. | |
gs_o() { | |
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \ | |
-dAutoRotatePages=/None \ | |
-dColorImageDownsampleType=/Bicubic \ | |
-dColorImageResolution="$DPI" \ | |
-dGrayImageDownsampleType=/Bicubic \ | |
-dGrayImageResolution="$DPI" \ | |
-dMonoImageDownsampleType=/Subsample \ | |
-dMonoImageResolution="$DPI" "$@" | |
} | |
printf %s 'Extracting text and images... ' | |
# Text | |
gs_o -o "$OVERLAY" -dFILTERIMAGE -dFILTERVECTOR "$INPUT" & | |
# Images | |
gs_o -o "$BACK" -dFILTERTEXT -dPDFSETTINGS=/screen "$INPUT" | |
wait | |
echo '[OK]' | |
printf %s 'Overlaying... ' | |
pdftk "$OVERLAY" multibackground "$BACK" output "$OUT" | |
echo '[OK]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment