0

I have a series of folders with employee names and their documents in pdf format. I want to merge and create a single PDF file with pdf documents indexed as bookmarks in pdf file.

I tried using PDFTK to merge the pdf's in folder, however the output does not bookmark.

1 Answers1

1

I created a script for this purpose. You may choose to keep .pdf extension by modify heredoc.

#!/usr/bin/env zsh

pages=1 bookmarks="bookmarks.txt" tmp_pdf="tmp.pdf"

if [ -f $bookmarks ]; then read -p "Do you want to overwrite the bookmarks file? [y/N] " -n 1 -r if [[ $REPLY =~ ^[Yy]$ ]]; then rm $bookmarks else echo "Aborting..." exit 1 fi fi

for pdf in *.pdf; do cat <<- EOF >> $bookmarks BookmarkBegin BookmarkTitle: ${pdf%.pdf} BookmarkLevel: 1 BookmarkPageNumber: $pages EOF page_number=$(pdftk $pdf dump_data | rg 'NumberOfPages: ' | awk '{print $2}') pages=$(($pages + $page_number)) done

pdftk *.pdf cat output $tmp_pdf pdftk $tmp_pdf update_info $bookmarks output final.pdf

rm -f $tmp_pdf rm -f $bookmarks

The idea is:

  • Create a pdftk info file which contains bookmark
  • Call pdftk cat to concatenate them
  • Call updateinfo to add bookmarks
  • Remove temp files, done.
Yubo
  • 121
  • thank you for your script, i am able to write this bit:tempPDF=mktemp for i in *.pdf do bookmarkTitle=basename "$i" .pdf bookmarkInfo="BookmarkBegin\nBookmarkTitle: $bookmarkTitle\nBookmarkLevel: 1\nBookmarkPageNumber: 1" pdftk "$i" update_info_utf8 <(echo -en $bookmarkInfo) output $tempPDF verbose mv $tempPDF "$i" done – Bibush GC Jul 18 '22 at 03:36
  • I keep trying to use this script, but I get a 33: parse error near \n, is there supposed to be more indentation in the pdf for loop? – nonreligious Mar 27 '23 at 11:06
  • Actually, I got it to work by removing the indentation for the whole EOF block. – nonreligious Mar 27 '23 at 11:49