Last active
September 6, 2023 14:14
-
-
Save lincerely/11cc8d32eed49eaf3a05203ea19b43bc to your computer and use it in GitHub Desktop.
add outline to PDF using ghostscript and PDFMark
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
#!/usr/bin/env bash | |
# add outline to PDF using ghostscript and PDFMark | |
# reference: https://www.meadowmead.com/wp-content/uploads/2011/04/PDFMarkRecipes.pdf | |
if [ $# -lt 2 ]; then | |
echo "usage: $0 INPDF OUTPDF < OUTLINE" | |
echo ' | |
Outline example: | |
1 Title | |
2 Subtitle 1 | |
3 Heading 1 | |
5 Heading 2 | |
6 Heading 3 | |
7 Subtitle 2 | |
Hierarchy is represented as indent level. | |
Noticed there is a tab between page number and outline name. | |
' | |
exit 1 | |
fi | |
INFILE="$1" | |
OUTFILE="$2" | |
awk ' | |
BEGIN { | |
FS="\t" | |
} | |
{ | |
level=NF-2 | |
pages[NR] = $(NF-1) | |
titles[NR] = $(NF) | |
if (level > prev_level) { # start of a child, prev is parent | |
parent[level] = NR-1 | |
} | |
childcount[parent[level]]++ | |
prev_level = level | |
} | |
END { | |
for (i = 1; i <= NR; i++) { | |
if (!childcount[i]) { | |
print("[ /Page "pages[i]" /Title ("titles[i]") /OUT pdfmark") | |
} else { | |
print("[ /Page "pages[i]" /Title ("titles[i]") /Count "childcount[i]" /OUT pdfmark") | |
} | |
} | |
} | |
' \ | |
| gs -o "$OUTFILE" -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress "$INFILE" - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment