Skip to main content

Adds bookmarks to PDF documents.

Project description

pdf-yaml-bookmark

Overview

pdf-yaml-bookmark is a command-line tool that adds PDF bookmarks (outlines or table of contents) using an indentation-based format. It is written in Python, uses PyYAML for indentation parsing, and Ghostscript for the actual execution of Adobe pdfmark commands.

The bookmark data format can be as simple as the following:

# my_bookmark.bkm
# This is a comment

First Chapter 1
    first section 1
        first subsection 1
    second section 4
    third section 5

# An offset to fix a gap between PDF pages and content pages
+10

Second Chapter 10
    some entry 10
    some entry 11

Why YAML?

pdf-yaml-bookmark uses YAML syntax as an intermediate representation for bookmark data, and it can also accept YAML directly. Here are a few reasons why we believe this is beneficial:

  • Robustness: Rather than writing a custom parser for indentation, we first convert the .bkm format into YAML. Since YAML parsers are well-maintained and reliable, this reduces bugs and makes the tool more robust.
  • Extensibility: You can use YAML's more advanced features, such as anchors and references, to manage large or repetitive bookmark trees.

Installation

Prerequisites

Make sure you have ghostscript installed.

On macOS, you can use homebrew:

$ brew install ghostscript
$ gs --version

For Windows, download and install ghostscript from the official website.

Installation

Install from PyPI:

$ pip install pdf-yaml-bookmark

Usage

pdf-yaml-bookmark my_document.pdf \
    --bookmark my_bookmark.bkm \
    --output my_bookmarked_document.pdf

This command adds bookmarks contained in my_bookmark.bkm to the file my_document.pdf and outputs the result as my_bookmarked_document.pdf.

Use the --show-progress option to display a progress bar, which is especially useful for PDF files with many pages:

$ pdf-yaml-bookmark my_document.pdf \
    --bookmark my_bookmark.bkm \
    --output my_bookmarked_document.pdf \
    --show-progress
 |██████████████████████----------------------------| 118/263

You can specify the Ghostscript executable path using the --gs-path option:

$ pdf-yaml-bookmark my_document.pdf \
    --bookmark my_bookmark.bkm \
    --output my_bookmarked_document.pdf \
    --gs-path 'C:\Program Files\gs\gs9.55.0\bin\gswin64.exe'

Note: If the bookmark file has a .yaml or .yml extension, it will be parsed as YAML, structured by keys heading, page, offset, and children. In that case, it might look like this:

# my_bookmark.yaml
# This is a comment
-
  heading: First Chapter
  page: 1
  offset: 0
  children:
    -
      heading: First section
      page: 1
      offset: 0
      children:
        -
          heading: Second section
          page: 1
          offset: 0
          children:
-
  heading: First Chapter
  page: 1
  offset: 5
  children:
    -
      heading: First section
      page: 1
      offset: 5
      children:
    -
      heading: Second section
      page: 1
      offset: 5
      children:

Run pdf-yaml-bookmark --help to see other available options.

Contributing

Issues and pull requests are welcome. Please read contributing.md before making a PR.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdf_yaml_bookmark-0.0.4.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdf_yaml_bookmark-0.0.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pdf_yaml_bookmark-0.0.4.tar.gz.

File metadata

  • Download URL: pdf_yaml_bookmark-0.0.4.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for pdf_yaml_bookmark-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c8f7fcbe22b06bfcd310c6d4e83151c35d5224bb81535a645f92a40008558a20
MD5 65196d70d820e9f6fe1f15e94424f59f
BLAKE2b-256 f93396a75c39fee3d26e26ed278dcc476a9d00f8269eca0058c471fff35f7b41

See more details on using hashes here.

File details

Details for the file pdf_yaml_bookmark-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pdf_yaml_bookmark-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bba16fd083d3d80e68dcae3ffa85b5d1750077766e221bb384193d62cff1c901
MD5 16a053e800b93576b1e8e280728daca6
BLAKE2b-256 75ca87a677dc063f1f76b1038e6b5aa151be2435fb1345080b8edce08609e55d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page