# pdf2tree

> Parse PDF and group elements based on enclosing lines.

Latest version **0.1.0** (published 2023-05-13) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install pdf2tree
pnpm add pdf2tree
yarn add pdf2tree
bun add pdf2tree
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-05-13 |
| First published | 2023-05-13 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | jogemu |
| Maintainers | jogemu |
| Keywords | pdf, parse, table |

## Links

- npm: https://www.npmjs.com/package/pdf2tree
- Repository: https://github.com/jogemu/pdf2tree
- Homepage: https://github.com/jogemu/pdf2tree#readme
- Issues: https://github.com/jogemu/pdf2tree/issues
- npm.io page: https://npm.io/package/pdf2tree

## Dependencies (1)

- [pdf2json](https://npm.io/package/pdf2json.md) ^3.0.3

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2023-05-13

## README

# pdf2tree
Parse PDF and group elements based on enclosing lines. A node.js module that promisifies the pdf2json parser and structures the data in a way that is suitable for tables with merged cells.

## How to use
After installing [node.js](https://nodejs.org) you can use npm to add pdf2tree in your project folder.

    npm install pdf2tree

When you create a new parser object as shown below, parameters are passed to the [pdf2json](https://github.com/modesty/pdf2json) parser.

    import PDF2Tree from 'pdf2tree'
    let pdf2tree = new PDF2Tree()

Then you can set the following pdf2tree specific parameters.

    pdf2tree.maxStrokeWidth = 1
    pdf2tree.maxGapWidth = 0.1

Finally, parsing can start either with a filepath or a buffer.

    pdf2tree.loadPDF(PDFpath)
    pdf2tree.parseBuffer(PDFbuffer)

The promise returns a JSON object as documented in [pdf2json](https://github.com/modesty/pdf2json), but adds an additional `Tree` property. To simplify readability `<str>` represents an object like the ones pdf2json provides for every Page but each object only contains all elements within the lines, i.e. `{ ..., Texts: [ { x, y, ..., R: [ { T: 'str', ... } ] } ], ... }`.

    {
      ...
      Tree: [
        [
          <Page 1>,
          [
            [ <A>, <B>, <C>, <D> ],
            [ <X>, <1>, <2>, <3> ],
            [ 
              <Y>,
              [
                [ <5>, <6>, <7> ],
                [ <8>, <9> ],
              ]
            ]
          ]
        ],
        [
          <Page 2>,
          [
            [ <TITLE> ],
            [
              <Z>, 
              [
                [
                  <F>,
                  <G>,
                  [
                    [ <H> ],
                    [ <I> ],
                  ],
                ],
                [ <J>],
                [ <K>]
              ],
              <?>
            ]
          ]
        ]
      ]
    }

For content structured like this:

    Page 1

    +---+---+---+---+
    | A | B | C | D |
    +---+---+---+---+
    | X | 1 | 2 | 3 |
    +---+---+---+---+
    |   | 5 | 6 | 7 |
    | Y +---+---+---+
    |   | 8 |   9   |
    +---+---+-------+

    Page 2
    
    +---+---+---+---+
    |     TITLE     |
    +---+---+---+---+
    |   |   |   | H |
    |   | F | G +---+
    |   |   |   | I |
    | Z +---+---+---+
    |   | J |       |
    |   +---+   ?   |
    |   | K |       |
    +---+---+-------+

If a cell is not rectangular or merges rows that the cell to the left did not also merge are not supported. This would require a data structure that allows traversing the neighborhood with `.right` or `.below` and can include loops for non-rectangular areas. It should be easier to fix those special cases after the parsing.

---
_Source: https://npm.io/package/pdf2tree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
