Skip to main content

PDF Bookmark Import and Export

Project description

PDF-Bookmark

PyPI

PDF-Bookmark is a tool for importing and exporting pdf bookmark with the bmk format.

Installation

$ pip install pdf-bookmark

pdf-bookmark utilizes PDFtk and Ghostscript to export and import pdf bookmarks. They must be installed before running pdf-bookmark.

PDFtk

PDFtk is used here to export bookmark from pdf file. The java port pdftk-java may also be OK.

On Arch Linux, pdftk-java could be installed by:

$ sudo pacman -S pdftk java-commons-lang

Verify the installation:

$ pdftk --version

Ghostscript

Ghostscript is used here to import bookmark to pdf file.

On Arch Linux, it could be installed by:

$ sudo pacman -S ghostscript

Verify the installation:

$ gs --version

bmk format

The bmk format is used to describe the bookmark of a pdf file. It will be used to import bookmark into a pdf file.

bmk format is easy to write. It looks quite like the content of a book. So you can copy the content and modify from it.

Each line represents a bookmark item. The title and the page number are separated by at least 4 dots ".".

The level of a bookmark is specified by the indentation of spaces. The default indentation is 2 spaces, and the number of spaces could be configured with inline command.

This is a simple example of a bmk file.

序................1
Chapter 1................4
Chapter 2................5
  2.1 Section 1................6
    2.1.1 SubSection 1................6
    2.1.2 SubSection 2................8
  2.2 Section 2................12
Chapter 3................20
Appendix................36

Import the bookmark and create a new pdf file:

$ pdf-bookmark -p input.pdf -b bookmark.bmk -o new.pdf

Export bmk format

The bmk format could also be exported from a pdf file with bookmark. You may also modify the bookmark from the exported one.

$ pdf-bookmark -p input.pdf

Inline command

There could also be inline commands in the file to do more controls on the bookmark. These commands start with !!! and modify some properties of bookmark. The new property will affect bookmarks after the line until it is changed again.

It is normal that the main body of a pdf file does not start from the first page of pdf, and the page number is not always arabic.

!!! collapse_level = 2

!!! num_style = Roman
Preface................I
Content................IV

!!! new_index = 12
!!! num_style = Arabic
Introduction................1
Chapter 1................4
Chapter 2................5
  2.1 Section 1................6
  2.2 Section 2................7
Chapter 3................10
Appendix................11

With these inline commands, you do not need to recalculate the index number for each page.

Here are all supported inline commands:

  • new_index. Default: 1. The following bookmark index will be recalculated from the new index number (new_index + page - 1).
  • num_start. Default: 1. Specify the number of first page if it does not start from 1 (new_index + page - num_start).
  • num_style. Default: Arabic. The page number style. Could be Arabic, Roman and Letters.
  • collapse_level. Default: 0. On which level the bookmarks are collapsed. 0 means expand all.
  • level_indent. Default: 2. Number of indentation spaces for a new level.

pdf-bookmark command

The pdf-bookmark command is installed by pip install pdf-bookmark.

usage: pdf-bookmark [-h] [-f {bmk,none,pdftk,pdfmark,json}]
                    [-l COLLAPSE_LEVEL] [-b BOOKMARK] [-p PDF] [-o OUTPUT_PDF]

Import and export PDF bookmark

optional arguments:
  -h, --help            show this help message and exit
  -f {bmk,none,pdftk,pdfmark,json}, --format {bmk,none,pdftk,pdfmark,json}
                        the output format of bookmark
  -l COLLAPSE_LEVEL, --collapse-level COLLAPSE_LEVEL
                        the min level to be collapsed, 0 to expand all
  -b BOOKMARK, --bookmark BOOKMARK
                        the bookmark file to be imported
  -p PDF, --pdf PDF     the input PDF file
  -o OUTPUT_PDF, --output-pdf OUTPUT_PDF
                        the output PDF file

Example

Import bookmark

This will import the bmk bookmark into a pdf file:

$ pdf-bookmark -p input.pdf -b bookmark.bmk -o new.pdf

If you would like to have a quite output:

$ pdf-bookmark -p input.pdf -b bookmark.bmk -f none -o new.pdf

Export bookmark

This will export the bmk bookmark to stdout from a pdf file:

$ pdf-bookmark -p input.pdf

The output format could be changed to pdfmark, json:

$ pdf-bookmark -p input.pdf -f pdfmark
$ pdf-bookmark -p input.pdf -f json

Change the collapse level

This will only change the collapse level of the pdf.

$ pdf-bookmark -p input.pdf -l 2 -o new.pdf

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-bookmark-1.1.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

pdf_bookmark-1.1.0-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pdf-bookmark-1.1.0.tar.gz.

File metadata

  • Download URL: pdf-bookmark-1.1.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.2

File hashes

Hashes for pdf-bookmark-1.1.0.tar.gz
Algorithm Hash digest
SHA256 381aeeb60588127c65ca91e8d356363b072ed2178464e5afa626a6855539c00d
MD5 c5340d6b8462620d8d94d4ca13730939
BLAKE2b-256 619638807d5ee950076ef48b2694c934a44d747693a53f6c5cf8af5b9da14c99

See more details on using hashes here.

File details

Details for the file pdf_bookmark-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pdf_bookmark-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.2

File hashes

Hashes for pdf_bookmark-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 64afc3085c152c91d8ba1612ca23dca1b422bbb18d273207bea45c65ff323ebf
MD5 1d055b934cad12cb9d98cc95b78ab38c
BLAKE2b-256 3dd672a8a13538115d4d2f51ed2a6b824bd6f1584bbf5f46d7f1effc19e33aa5

See more details on using hashes here.

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