Skip to main content

Automatically generate table of contents for pdf files

Project description

pdf.tocgen

                          in.pdf
                            |
                            |
     +----------------------+--------------------+
     |                      |                    |
     V                      V                    V
+----------+          +-----------+         +----------+
|          |  recipe  |           |   ToC   |          |
| pdfxmeta +--------->| pdftocgen +-------->| pdftocio +---> out.pdf
|          |          |           |         |          |
+----------+          +-----------+         +----------+

pdf.tocgen is a set of command-line tools for automatically extracting and generating the table of contents (ToC) of a PDF file. It uses the embedded font attributes and position of headings to deduce the basic outline of a PDF file.

It works best for PDF files produces from a TeX document using pdftex (and its friends pdflatex, pdfxetex, etc.), but it's designed to work with any software-generated PDF files (i.e. you shouldn't expect it to work with scanned PDFs). Some examples include troff/groff, Adobe InDesign, Microsoft Word, and probably more.

Please see the homepage for a detailed introduction.

Installation

pdf.tocgen written in Python 3. It is known to work with Python 3.8 under Linux, but Python 3.7 should be the minimum. Use

$ pip install -U pdf.tocgen

to install the latest version systemwide, or use

$ pip install -U --user pdf.tocgen

to install it for the current user. I would recommend the latter approach to avoid messing up the package managers on your system.

Workflow

The design of pdf.tocgen is influenced by the Unix philosophy. I intentionally separated pdf.tocgen to 3 separate programs. They work together, but each of them is useful on their own.

  1. pdfxmeta: extract the metadata (font attributes, positions) of headings to build a recipe file.
  2. pdftocgen: generate a table of contents from the recipe.
  3. pdftocio: import the table of contents to the PDF document.

You should read the example on the homepage for a proper introduction, but the basic workflow follows like this.

First, use pdfxmeta to search for metadata of headings

$ pdfxmeta -p page in.pdf pattern >> recipe.toml
$ pdfxmeta -p page in.pdf pattern2 >> recipe.toml

Edit the recipe.toml file to pick out the attributes you need and specify the heading levels.

$ vim recipe.toml # edit

An example recipe would look like this:

[[filter]]
level = 1
font.name = "Times-Bold"
font.size = 19.92530059814453

[[filter]]
level = 2
font.name = "Times-Bold"
font.size = 11.9552001953125

Then pass the recipe to pdftocgen to generate a table of contents,

$ pdftocgen in.pdf < recipe.toml
"Preface" 5
    "Bottom-up Design" 5
    "Plan of the Book" 7
    "Examples" 9
    "Acknowledgements" 9
"Contents" 11
"The Extensible Language" 14
    "1.1 Design by Evolution" 14
    "1.2 Programming Bottom-Up" 16
    "1.3 Extensible Software" 18
    "1.4 Extending Lisp" 19
    "1.5 Why Lisp (or When)" 21
"Functions" 22
    "2.1 Functions as Data" 22
    "2.2 Defining Functions" 23
    "2.3 Functional Arguments" 26
    "2.4 Functions as Properties" 28
    "2.5 Scope" 29
    "2.6 Closures" 30
    "2.7 Local Functions" 34
    "2.8 Tail-Recursion" 35
    "2.9 Compilation" 37
    "2.10 Functions from Lists" 40
"Functional Programming" 41
    "3.1 Functional Design" 41
    "3.2 Imperative Outside-In" 46
    "3.3 Functional Interfaces" 48
    "3.4 Interactive Programming" 50
[--snip--]

which can be directly imported to the PDF file using pdftocio,

$ pdftocgen in.pdf < recipe.toml | pdftocio -o out.pdf in.pdf

Or if you want to edit the table of contents before importing it,

$ pdftocgen in.pdf < recipe.toml > toc
$ vim toc # edit
$ pdftocio in.pdf < toc

Each of the three programs has some extra functionalities. Use the -h option to see all the options you could pass in.

Development

If you want to modify the source code or contribute anything, first install poetry, which is a dependency and package manager for Python used by pdf.tocgen. Then run

$ poetry install

in the root directory of this repository to set up development dependencies.

If you want to test the development version of pdf.tocgen, use the poetry run command:

$ poetry run pdfxmeta in.pdf "pattern"

Alternatively, you could also use the

$ poetry shell

command to open up a virtual environment and run the development version directly:

(pdf.tocgen) $ pdfxmeta in.pdf "pattern"

Before you send a patch or pull request, make sure the unit test passes by running:

$ make test

License

pdf.tocgen is free software. The source code of pdf.tocgen is licensed under the GNU GPLv3 license.

pdf.tocgen is based on PyMuPDF, licensed under the GNU GPLv3 license, which is again based on MuPDF, licensed under the GNU AGPLv3 license. A copy of the AGPLv3 license is included in the repository.

If you want to make any derivatives based on this project, please follow the terms of the GNU GPLv3 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.tocgen-0.9.9.tar.gz (37.5 kB view hashes)

Uploaded Source

Built Distribution

pdf.tocgen-0.9.9-py3-none-any.whl (40.5 kB view hashes)

Uploaded Python 3

Supported by

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