Created
April 25, 2024 14:08
Generate A Markdown TOC from a pdf via this bash function (Useful for Obsidian Note Taking)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: generate_markdown_toc "AS 3000-2018 Wiring Rules.pdf" 3 | |
generate_markdown_toc() { | |
local pdf_file="$1" | |
local max_level="$2" | |
pdftk "$pdf_file" dump_data | awk -v max_level="$max_level" '/BookmarkTitle:/ {gsub("BookmarkTitle: ", ""); title=$0} /BookmarkPageNumber:/ {gsub("BookmarkPageNumber: ", ""); page=$0} /BookmarkLevel:/ {gsub("BookmarkLevel: ", ""); level=$0; if (level <= max_level) printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)}' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's is awk but broken up to make it easier to understand
Here is the awk code specifically isolated for syntax highlighting