diff --git a/README.md b/README.md index 02d69ab..d353a2e 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,8 @@ This repository holds shell scripts that Intersect uses to engage in Cardano on- - [hash.sh](./scripts/hash.sh) - Performs a blake2b-256 hash on provided file +- [pdf-remove-metadata.sh](./scripts/pdf-remove-metadata.sh) + - Removes PDF metadata from PDF files ### Documentation diff --git a/scripts/author-create.sh b/scripts/author-create.sh index 5369139..ab34e93 100755 --- a/scripts/author-create.sh +++ b/scripts/author-create.sh @@ -51,7 +51,6 @@ while [[ $# -gt 0 ]]; do new_file="true" shift ;; - -h|--help) usage ;; diff --git a/scripts/pdf-remove-metadata.sh b/scripts/pdf-remove-metadata.sh new file mode 100755 index 0000000..41e5b54 --- /dev/null +++ b/scripts/pdf-remove-metadata.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +################################################## +DEFAULT_NEW_FILE="false" # default to false, to the script overwrites the input file +################################################## + +# Exit immediately if a command exits with a non-zero status, +# treat unset variables as an error, and fail if any command in a pipeline fails +set -euo pipefail + +# check if user has exiftool cli installed +if ! command -v exiftool >/dev/null 2>&1; then + echo "Error: exiftool cli is not installed or not in your PATH." >&2 + exit 1 +fi + +# check if user has qpdf cli installed +if ! command -v qpdf >/dev/null 2>&1; then + echo "Error: qpdf cli is not installed or not in your PATH." >&2 + exit 1 +fi + +# Usage message +usage() { + echo "Usage: $0 [--new-file]" + echo "Remove all metadata from a PDF file and set the Title metadata to the filename." + echo " " + echo "Options:" + echo " Path to your PDF file." + echo " --new-file Create a new file with the signed metadata (default: $DEFAULT_NEW_FILE)" + exit 1 +} + +# Initialize variables with defaults +input_path="" +new_file="$DEFAULT_NEW_FILE" + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + --new-file) + new_file="true" + shift + ;; + -h|--help) + usage + ;; + *) + if [ -z "$input_path" ]; then + input_path="$1" + fi + shift + ;; + esac +done + +# Check for required arguments +if [ -z "$input_path" ]; then + echo "Error: Could not find PDF file." + usage +fi + +# Extract filename without extension +BASENAME=$(basename "$input_path" .pdf) + +# could add some logic here +TITLE=$(echo "$BASENAME") + +if [ "$new_file" = "true" ]; then + output_new_file="$input_path-new-metadata.pdf" + # remove all metadata from the original PDF and create a new PDF + exiftool -all= "$input_path" -o "$output_new_file" + # remove all hidden metadata + qpdf --linearize "$input_path" --replace-input + # add the Title metadata to the new PDF + exiftool -Title="$TITLE" "$output_new_file" -overwrite_original_in_place + echo "Metadata processing completed. New file: $output_new_file with Title: $TITLE" +else + # remove all metadata from the original PDF + exiftool -all= "$input_path" -overwrite_original_in_place + # remove all hidden metadata from the original PDF + qpdf --linearize "$input_path" --replace-input + # add the Title metadata to the original PDF + exiftool -Title="$TITLE" "$input_path" -overwrite_original_in_place + echo "Metadata processing completed. Your file: $input_path now only has a Title: $TITLE" +fi