dotfiles/hm/scripts/overpdf

67 lines
2.2 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env nix-shell
2024-09-02 16:26:21 +02:00
#! nix-shell -i bash
#! nix-shell -p bash pdftk inkscape gnused coreutils file
2020-01-18 23:17:09 +01:00
2024-09-02 16:26:21 +02:00
set -euxo pipefail
2020-01-18 23:17:09 +01:00
# Utility to write over a PDF file pages
# TODO Inkscape vodoo: Put the original in its own layer and skip when merging
orig_path="$1"
orig_dir="$(dirname "$orig_path")"
orig_file="$(basename "$orig_path")"
orig_ext="${orig_file##*.}"
orig_name="${orig_file%.*}"
wdir_file="${orig_name}_src.${orig_ext}"
wdir_path="${orig_dir}/${wdir_file}"
if [ -d "$wdir_path" ]
then
echo "Source directory $wdir_path found"
2020-01-21 21:39:21 +01:00
ls "${wdir_path}/"*"_og.pdf" | while read page_orig_path
2020-01-18 23:17:09 +01:00
do
page_stmp_svg="$(echo "$page_orig_path" | sed 's|_og\.pdf$|_fg\.svg|')"
page_stmp_pdf="$(echo "$page_orig_path" | sed 's|_og\.pdf$|_fg\.pdf|')"
page_fin_pdf="$(echo "$page_orig_path" | sed 's|_og\.pdf$|_fin\.pdf|')"
if [ -f "$page_stmp_svg" ]
then
echo "Processing $page_orig_path (applying stamp)"
2020-08-08 11:19:48 +02:00
inkscape "$page_stmp_svg" --export-filename "$page_stmp_pdf"
2020-01-18 23:17:09 +01:00
pdftk "$page_orig_path" stamp "$page_stmp_pdf" output "$page_fin_pdf"
else
echo "Processing $page_orig_path (copying)"
cp "$page_orig_path" "$page_fin_pdf"
fi
done
echo "Merging everything back to ${orig_path}."
2020-01-21 21:39:21 +01:00
pdftk "${wdir_path}/"*"_fin.pdf" output "$orig_path"
2020-01-18 23:17:09 +01:00
echo "Deleting temporary files."
2020-01-21 21:39:21 +01:00
rm "${wdir_path}/"*"_fin.pdf" "${wdir_path}/"*"_fg.pdf"
2020-01-18 23:17:09 +01:00
echo "Done."
elif [ -f "$orig_path" ]
then
2020-05-03 13:24:45 +02:00
if [ "$(file --mime-type --brief "$orig_path")" != "application/pdf" ]
2020-01-18 23:17:09 +01:00
then
echo "${orig_path}: not a PDF file"
exit 1
fi
echo "Creating source directory $wdir_path with original pages and template SVGs"
mkdir "$wdir_path"
pdftk "$orig_file" burst output "${wdir_path}/${orig_name}_%03d_og.pdf"
2020-01-21 21:39:21 +01:00
ls "${wdir_path}/"*"_og.pdf" | while read page_orig_path
2020-01-18 23:17:09 +01:00
do
page_stmp_svg="$(echo "$page_orig_path" | sed 's|_og\.pdf$|_fg\.svg|')"
echo "Processing $page_orig_path"
2020-08-08 11:19:48 +02:00
inkscape "$page_orig_path" --export-plain-svg --export-filename "$page_stmp_svg"
2020-01-18 23:17:09 +01:00
done
echo "Done. Make sure to edit in a a new layer in Inkscape and hide the original one."
else
echo "${orig_path}: no such file or directory"
exit 1
fi