TransWikia.com

Merge pdf files and automatically create a table of contents with each file as an entry

Unix & Linux Asked by Seninha on December 8, 2020

I have several pdf files (chapter1.pdf, chapter2.pdf, etc.), each one being a chapter of a book. I now how to merge them into a single pdf (I use the command pdfunite from poppler), but since the output file is big, it’s difficult to find a chapter without having them indexed in a table of contents. So how to create an embedded table of contents in which each merged chapter is an entry?

Note that I do not want to create a page in the output file which contains the list of chapters and their respective page numbers. I want the index/table of contents metadata of an pdf file, that can be browseable in any pdf reader’s (or ebook device’s) which supports such feature.

2 Answers

Non-destructive version of @bu5hman's answer:

#!/bin/bash

out_file="combined.pdf"
tmp_dir="/tmp/pdftk_unite"
bookmarks_file="$tmp_dir/bookmarks.txt"
bookmarks_fmt="BookmarkBegin
BookmarkTitle: %s
BookmarkLevel: 1
BookmarkPageNumber: 1
"

rm -rf "$tmp_dir"
mkdir -p "$tmp_dir"

for f in *.pdf; do
    echo "Bookmarking $f..."
    title="${f%.*}"
    printf "$bookmarks_fmt" "$title" > "$bookmarks_file"
    pdftk "$f" update_info "$bookmarks_file" output "$tmp_dir/$f"
done

pdftk "$tmp_dir"/*.pdf cat output "$out_file"

Answered by Mateen Ulhaq on December 8, 2020

A function I use all the time to do exactly this. Just make sure the pdfs sort properly in sequence in the expansion.

tp="/tmp/tmp.pdf"
td="/tmp/data"
for i in *.pdf; do
    echo "Bookmarking $i"
    printf "BookmarkBeginnBookmarkTitle: %snBookmarkLevel: 1nBookmarkPageNumber: 1n" "${i%.*}"> "$td"
    pdftk "$i" update_info "$td" output "$tp"
    mv "$tp" "$i"
done
pdftk *.pdf cat output myBook.pdf

Answered by bu5hman on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP