Bash Script to add bookmarks to PDFs

Updated . Posted . Visible to the public.

This uses pdftk.

#!/bin/bash

file=$1
text=$2

if [ -z "$file" ] | [ -z "$text" ]; then
  echo "Usage: sh script.sh file.pdf 'text for bookmark'"
  exit 1
fi

echo "File:    $file"
echo "Text:    $text"

outfile=$("basename" "$file" ".pdf")_mit_Inhaltsverzeichnis.pdf
echo "Outfile: $outfile"

data=$("pdftk" "$file" "dump_data")
#echo "$data"
#echo 

new_part=$(cat << EOF
BookmarkBegin
BookmarkTitle: $text 
BookmarkLevel: 1
BookmarkPageNumber: 1
EOF
)
#echo "$new_part"
#echo

new_data="$data""\n""$new_part"
echo "$new_data" > new_data.txt

pdftk "$file" update_info_utf8 new_data.txt output "$outfile"
rm new_data.txt
Judith Roth
Last edit
Judith Roth
Posted by Judith Roth to Judith's Dev Notes (2021-09-30 21:07)