# pdftojson

> pdftotext wrapper that generates JSON with bounding box data. Takes care of duplicate characters.

Latest version **0.0.3** (published 2015-07-15) · MIT license · 0 weekly downloads

## Install

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

Provides the command `pdftojson`.

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2015-07-15 |
| First published | 2015-07-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 14 |
| Author | MrOrz |
| Maintainers | mrorz |
| Keywords | pdftotext |

## Links

- npm: https://www.npmjs.com/package/pdftojson
- Repository: https://github.com/MrOrz/pdftojson
- Issues: https://github.com/MrOrz/pdftojson/issues
- npm.io page: https://npm.io/package/pdftojson

## Dependencies (6)

- [tmp](https://npm.io/package/tmp.md) 0.0.26
- [debug](https://npm.io/package/debug.md) ^2.2.0
- [yargs](https://npm.io/package/yargs.md) ^3.15.0
- [cheerio](https://npm.io/package/cheerio.md) ^0.19.0
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^5.6.15
- [es6-promisify](https://npm.io/package/es6-promisify.md) ^2.0.0

## 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.0.3 (latest) — 2015-07-15
- 0.0.2 — 2015-07-11
- 0.0.1 — 2015-07-11

## README

pdftojson
=========

[![Build Status](https://travis-ci.org/MrOrz/pdftojson.svg)](https://travis-ci.org/MrOrz/pdftojson) [![Coverage Status](https://coveralls.io/repos/MrOrz/pdftojson/badge.svg?branch=master&service=github)](https://coveralls.io/github/MrOrz/pdftojson?branch=master)

**pdftojson** is a `pdftotext` wrapper that generates JSON with bounding box data. It takes care of overlapping duplicated characters, which often exists in MS-Word-generated PDF files with floating images and text.

Why bother a wrapper for `pdftotext`?
------------------------------

Consider this PDF file:

![PDF sample](http://i.imgur.com/MRGE2bq.png)

`pdftotext -bbox theFile.pdf` would generate this:

```html
...
<word xMin="103.320000" yMin="547.355700" xMax="152.368008" yMax="561.321720">(6)綠線</word>
<word xMin="155.880000" yMin="547.355700" xMax="176.846541" yMax="561.321720">G01</word>
<word xMin="155.880000" yMin="547.355700" xMax="162.867200" yMax="561.321720">G</word>
<word xMin="180.300000" yMin="547.355700" xMax="222.295867" yMax="561.321720">站延伸</word>
<word xMin="208.080000" yMin="547.355700" xMax="264.053062" yMax="561.321720">伸至大溪</word>
<word xMin="264.480000" yMin="547.355700" xMax="334.420485" yMax="561.321720">、龍潭先進</word>
<word xMin="320.340000" yMin="547.355700" xMax="348.294390" yMax="561.321720">進公</word>
<word xMin="124.680000" yMin="572.375700" xMax="166.675867" yMax="586.341720">共運輸</word>
<word xMin="152.700000" yMin="572.375700" xMax="222.644667" yMax="586.341720">輸系統發展</word>
<word xMin="208.440000" yMin="572.375700" xMax="278.395867" yMax="586.341720">展委託可行</word>
<word xMin="264.840000" yMin="572.375700" xMax="320.813062" yMax="586.341720">行性研究</word>
...
```

`pdftotext` does a great job "undoing" physical layout (columns, hyphenation, etc) of a PDF document. However, in its result there are some overlapping and duplicate words. PDF layout engines sometimes generate these quirks when images and text are mixed within a page.

On the other hand, `pdftojson theFile.pdf` could generate this:

```js
...
{
    "xMin": 103.2,
    "xMax": 348.29439,
    "yMin": 547.3557,
    "yMax": 561.32172,
    "text": "(6)綠線 G01 站延伸至大溪、龍潭先進公"
},
{
    "xMin": 124.68,
    "xMax": 320.813062,
    "yMin": 572.3757,
    "yMax": 586.34172,
    "text": "共運輸系統發展委託可行性研究"
}
...
```

Install
-------

```
$ npm install pdftojson
```

`pdftojson` uses [`pdftotext`](http://www.foolabs.com/xpdf/home.html). Please make sure `pdftotext` is available in `PATH`.


Usage
-----

pdftojson is available as a command line tool and a nodejs library.

### CLI

```
# outputs some.json
$ pdftojson some.pdf

# converts page 3 ~ 6 of some.pdf and outputs to some.json
$ pdftojson -c "-f 3 -l 6" some.pdf
```

### NodeJS Library

The library exposes a single function that takes the name of a PDF file
and returns a promise.

```js
import pdftojson from 'pdftojson';

pdftojson("./some.pdf").then((output) => {
  // output is a Javascript object.
});
```

### Output format

All numeric values are in `pt`.

```js
[
  { //: Page
    width: (Number) page width,
    height: (Number) page height,
    words: [
      {
        text: (String) the text enclosed in the bounding box,

        // All coordinates calculated from top-left corner of the page
        xMin: (Number) left edge of the bounding box,
        xMax: (Number) right edge of the bounding box,
        yMin: (Number) top edge of the bounding box,
        yMax: (Number) bottom edge of the bounding box
      }, // ...
    ]
  }, // ...
]
```

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