npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdftoc

v0.1.1

Published

Creates Table of Contents in PDF documents

Downloads

3

Readme

npm pipeline status

logo Pdftoc

Creates Table of Contents in PDF documents

Usage

  1. Clone this project
  2. Run npm run build
  3. Run tool via npx pdftoc

Recipes

Recipes describe how to detect headings in the PDF. There are basically two strategies, which may be combined:

  1. By Font: Headings are often rendered with a unique font and can therefor detected by font.
  2. By Pattern: Headings may also be detected by reqular expressions, e.g., they often start with numbers like "2.3.1".

The recipe file is a json file consisting of an array of recipies. Each recipe may contain the following properties, of which all except level are optional.

  • fontName: the name of the font as used in the PDF file
  • fontSizeFrom: The minimum size of the font (in points), e.g. 30
  • fontSizeTo: The maximums size of the font (in points), if not defined, the from size is used here as well
  • bold: true or false
  • italic: true or false
  • fillRGBColor: Array of three numbers with the RGB colors (from 0 to 255), e.g. [200,0,0] for red.
  • regexp: Regular expression applied on text, e.g. "^\d+\.\d+\.\d+\s+.*". Note that backslashes are to be escaped! The regexp could either be a single regex or an array. In the latter case, the regexes are applied to the first text matching the font filters and all succeeding texts (without checking the fonts).
  • level: The level, starting from 1 to whatever.

Hint: If you use a regular expressions, it might happen that the table of content of the document itself duplicates all heading. In that case, set pages accordingly, e.g. pages="5-", so that the table of content pages are ignored.

Examples

Detect headings based on font:

[
    { "fontName": "BHTCaseMicro", "fontSizeFrom": 64, 
        "bold": true, "italic": false, "level": 1 },
    { "fontName": "BHTCaseMicro", "fontSizeFrom": 60, 
        "bold": true, "italic": false, "level": 2 },
    { "fontName": "BHTCaseMicro", "fontSizeFrom": 52, 
        "bold": true, "italic": false, "level": 3 },
    { "fontName": "BHTCaseMicro", "fontSizeFrom": 48, 
        "bold": true, "italic": false, "level": 4 },
]

Detect heading based on regular expression:

[
    { "regexp": "^\\d+\\s+.*", "level": 1 },
    { "regexp": "^\\d+\\.\\d+\\s+.*",  "level": 2 },
    { "regexp": "^\\d+\\.\\d+\\.\\d+\\s+.*", "level": 3 },

]

Tip: Using regular expressions with "bold": true works in many cases :-)

Implementation Notes

PDF.js

  • Documentation of operations: https://github.com/MeiKatz/pdfjs-docs
  • PDF 2.0 Specification: https://developer.adobe.com/document-services/docs/assets/5b15559b96303194340b99820d3a70fa/PDF_ISO_32000-2.pdf
  • PDF 1.7 Specification: https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf

Modules

This package relies on PDF.js, which is added as dependency (pdfjs-dist). This package uses ES modules, and as a consequence ES modules are used here as well. There are some problems with that, in particular with TypeScript:

  1. TypeScript files either need a file extension .mts or, as done here, we defined type="module" in package.json
  2. TS-Jest works with ES modules, but it is a bit of a hassle. See https://jestjs.io/docs/ecmascript-modules and https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/#support-mts-extension

As noted at the PDF.js-FAQ, we need to import the legacy build of PDF.js.

Status

In development. Works with some files, sometimes font is not detected. In order to use fonts in settings, usually access to original document with font information is required (as the analysis is not really usable at the moment). But using regex works quite well even for unknown documents.

Probably need a GUI sooner or later ;-)

License

This program and the accompanying materials are made available under the terms of the Eclipse Public License v. 2.0 which is available at https://www.eclipse.org/legal/epl-2.0.