如何给 PDF 添加书签

Python的字符编码,二进制、八进制、十进制、十六进制和emoji https://blog.csdn.net/puran1218/article/details/105014713

Linux 下,用命令行给 PDF 添加书签 https://www.xianmin.org/post/linux-pdftk/ Run in the shell.

  • pdftk my.pdf dump_data output data.txt
  • pdftk my.pdf update_info data.txt output bookmarked.pdf
# https://www.xianmin.org/post/linux-pdftk/
# Run in the shell.
#   - `pdftk my.pdf dump_data output data.txt` 
#   - `pdftk my.pdf update_info data.txt output bookmarked.pdf`
# https://blog.csdn.net/puran1218/article/details/105014713

# 用 gs 压缩 PDF 文件
# https://blog.csdn.net/huludan/article/details/104105546
# gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -sOutputFile=output.pdf input.pdf

def ch_to_html_encoding(s: str) -> str:
    literal = []
    for c in s:
        literal.append("&#" + str(ord(c)))
    return ";".join(literal) + ";�"

def to_bookmark(title: str, level: int, page: int) -> str:
    return "BookmarkBegin\nBookmarkTitle: {title}�\nBookmarkLevel: {level}\nBookmarkPageNumber: {page}".format(
        title=ch_to_html_encoding(title), level=level, page=page)

f = open("toc.txt", "r", encoding="utf-8")
for line in f:
    txt = line.strip('\n')
    level = 0
    while txt[level] == ' ':
        level = level + 1

    s_end = len(txt)-1
    while txt[s_end] != '-':
        s_end = s_end - 1
    page = txt[s_end+1:]

    s_end = s_end-2
    s_start = level
    title = txt[s_start:s_end]
    print(to_bookmark(title, int(level*0.25)+1, int(page)+10))

f.close()

用 gs 压缩 PDF 文件

Ghostscript压缩、分割、合并PDF https://blog.csdn.net/huludan/article/details/104105546

由于 Git LFS 不支持中文,所以只能压缩 PDF 文件了。

gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dQUIET -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -sOutputFile=output.pdf input.pdf