Skip to content

Instantly share code, notes, and snippets.

@anton0xf
Last active January 10, 2022 09:05
Show Gist options
  • Save anton0xf/e00c766f33534b92b3b019eb4d813127 to your computer and use it in GitHub Desktop.
Save anton0xf/e00c766f33534b92b3b019eb4d813127 to your computer and use it in GitHub Desktop.
Convert evince bookmarks to PDF outline

extract bookmarks:

$ gio info -a "metadata::evince::bookmarks" coq-art.pdf \
    | sed -n '/metadata::/ s/^.*: //p' > coq-art.bookmarks

and restore its by:

gio set coq-art.pdf 'metadata::evince::bookmarks' "$(cat coq-art.bookmarks)"

see https://unix.stackexchange.com/questions/413769/extract-evinces-bookmarks-from-a-pdf-and-add-them-to-another

Convert bookmarks to outline. See https://www.pdflabs.com/blog/export-and-import-pdf-bookmarks/

$ pdftk coq-art.pdf dump_data output coq-art.data
$ <coq-art.bookmarks sed 's/^\[//; s/\]$//' \
    | sed "s/(\([0-9]*\), '[0-9IVX]*\s*- \([^']*\)')\(, \)\?/\1\t\2\n/g" | sed '/^\s*$/d' | sort -n > coq-art.bookmarks.tsv
$ <coq-art.bookmarks.tsv awk -F'\t' '$2 != "" {print "BookmarkBegin"; 
print "BookmarkTitle:", $2; print "BookmarkLevel:", ($2 ~ /^[0-9]+\.[0-9]/) ? 2 : 1;
print "BookmarkPageNumber:", $1 + 1}' > coq-art.bookmarks.data
$ cat coq-art.data coq-art.bookmarks.data > coq-art2.data
$ pdftk coq-art.pdf update_info coq-art2.data output coq-art2.pdf
@anton0xf
Copy link
Author

anton0xf commented May 18, 2021

Head of gio info -a "metadata::evince::bookmarks" coq-art.pdf:

uri: file:///home/anton0xf/doc/books/dev/coq/Coq%E2%80%99Art%20-%20Yves%20Bertot,%20Pierre%20Cast%C3%A9ran.pdf
attributes:                                        
  metadata::evince::bookmarks: [(15, 'XVII - ToC'), (24, '001 - 1. A Brief Overview'), (7, 'IX      - Foreword'), (4, 'V       - Pr
eface'), (23, 'XXV - ToC end'), (472, '453 - References'), ...

@anton0xf
Copy link
Author

Juick post.
Point post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment