Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion scripts/author-create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ while [[ $# -gt 0 ]]; do
new_file="true"
shift
;;

-h|--help)
usage
;;
Expand Down
86 changes: 86 additions & 0 deletions scripts/pdf-remove-metadata.sh
Original file line number Diff line number Diff line change
@@ -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 <pdf-file> [--new-file]"
echo "Remove all metadata from a PDF file and set the Title metadata to the filename."
echo " "
echo "Options:"
echo " <pdf-file> 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