# @ridi/pdf-parser

> Common PDF data parser for Ridibooks services

Latest version **0.7.4-alpha.1** (published 2021-06-03) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @ridi/pdf-parser
pnpm add @ridi/pdf-parser
yarn add @ridi/pdf-parser
bun add @ridi/pdf-parser
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.4-alpha.1 |
| Published | 2021-06-03 |
| First published | 2019-07-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 42.7 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 28 |
| Author | Ridibooks Viewer Team |
| Maintainers | hw.choi, ryan.kim, ridi-junhee, minq, limeburst, m0ai, davin.ahn, ridicorp, sb.lee |

## Links

- npm: https://www.npmjs.com/package/@ridi/pdf-parser
- Repository: https://github.com/ridi/content-parser
- Issues: https://github.com/ridi/content-parser/issues
- npm.io page: https://npm.io/package/@ridi/pdf-parser

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [pdfjs-dist](https://npm.io/package/pdfjs-dist.md) ^2.7.570
- [@ridi/parser-core](https://npm.io/package/@ridi/parser-core.md) 0.7.4-alpha.1

## Recent versions

- 0.7.4-alpha.1 (latest) — 2021-06-03
- 0.7.4-alpha.0 — 2021-06-03
- 0.7.3 — 2021-06-02
- 0.7.3-alpha.3 — 2021-06-02
- 0.7.3-alpha.2 — 2021-06-02
- 0.7.3-alpha.1 — 2021-06-02
- 0.7.3-alpha.0 — 2021-06-02
- 0.7.2 — 2021-06-02
- 0.7.2-alpha.4 — 2020-12-09
- 0.7.2-alpha.3 — 2020-12-01
- 0.7.2-alpha.2 — 2020-11-18
- 0.7.2-alpha.1 — 2020-11-18
- 0.7.2-alpha.0 — 2020-11-18
- 0.7.1 — 2020-11-13
- 0.7.0 — 2020-10-31
- … 27 more at https://npm.io/package/@ridi/pdf-parser/versions

## README

# @ridi/pdf-parser

> Common PDF data parser for Ridibooks services

[![NPM version](https://badge.fury.io/js/%40ridi%2Fpdf-parser.svg)](https://badge.fury.io/js/%40ridi%2Fpdf-parser)
[![Check](https://github.com/ridi/content-parser/actions/workflows/check.yml/badge.svg)](https://github.com/ridi/content-parser/actions/workflows/check.yml)
[![codecov](https://codecov.io/gh/ridi/content-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/ridi/content-parser)
[![NPM total downloads](https://img.shields.io/npm/dt/%40ridi%2Fpdf-parser.svg)](https://npm.im/%40ridi%2Fpdf-parser)

## Features

- [x] Structure parsing
- [ ] Read files
  - [ ] Read cover page
- [x] Encrypt and decrypt function when parsing or reading
- [ ] Debug mode
- [ ] Environment
  - [x] Node
  - [ ] CLI
  - [ ] Browser
- [ ] Online demo

## Install

```bash
npm install @ridi/pdf-parser
```

## Usage

Basic:

```js
import { PdfParser } from '@ridi/pdf-parser';
// or const { PdfParser } = require('@ridi/pdf-parser');

const parser = new PdfParser('./foo/bar.pdf');
parser.parse().then((book) => {
  ...
});
parser.read().then((pdfFileBuffer) => {
  ...
});
```

with [AesCryptor](https://github.com/ridi/content-parser/blob/master/src/cryptor/AesCryptor.js):

```js
import { CryptoProvider, AesCryptor } from '@ridi/pdf-parser';
// or const { CryptoProvider, AesCryptor } = require('@ridi/pdf-parser');

const { Purpose } = CryptoProvider;
const { Mode, Padding } = AesCryptor;

class ContentCryptoProvider extends CryptoProvider {
  constructor(key) {
    super();
    this.cryptor = new AesCryptor(Mode.ECB, { key });
  }

  getCryptor(filePath, purpose) {
    return this.cryptor;
  }

  // If use as follows:
  // const provider = new ContentCryptoProvider(...);
  // const parser = new PdfParser('encrypted.pdf', provider);
  // const book = await parser.parse();
  // const cover = await parser.readItem(book.cover);
  //
  // It will be called as follows:
  // 1. run(data, 'encrypted.pdf', Purpose.READ_IN_DIR)
  // 2. run(data, 'encrypted.pdf', Purpose.READ_IN_DIR)
  //
  run(data, filePath, purpose) {
    const cryptor = this.getCryptor(filePath, purpose);
    if (purpose === Purpose.READ_IN_DIR) {
      return cryptor.decrypt(data, { padding: Padding.AUTO });
    }
    return data;
  }
}

const cryptoProvider = new ContentCryptoProvider(key);
const parser = new PdfParser('./foo/bar.pdf', cryptoProvider);
```

Log level setting:

```js
import { LogLevel, ... } from '@ridi/pdf-parser';
const parser = new PdfParser(/* path */, /* cryptoProvider */, /* logLevel */)
// or const parser = new PdfParser(/* path */, /* logLevel */)
parser.logger.logLevel = LogLevel.VERBOSE; // SILENT, ERROR, WARN(default), INFO, DEBUG, VERBOSE
```

## API

### parse(parseOptions)

Returns `Promise<PdfBook>` with:

- [PdfBook](#book): Instance with pages info.

Or throw exception.

#### [parseOptions](#parseOptions): `?object`

---

### read()

Returns PDF file as `Buffer`.

---

### onProgress = callback(step, totalStep, action)

Tells the progress of parser through `callback`.

```js
const { Action } = PdfParser; // PARSE, READ_ITEMS
parser.onProgress = (step, totalStep, action) => {
  console.log(`[${action}] ${step} / ${totalStep}`);
}
```

## Model

<a id="book"></a>

### [PdfBook](./src/model/Book.js)

- version: *[Version](#version)*
- title: *string*
- author: *string*
- subject: *string*
- keywords: *string*
- creator: *string*
- producer: *string*
- creationDate: *?string*
- modificationDate: *?string*
- outlineItems: *[OutlineItem](#outlineItem)[]*
- isLinearized: *boolean*
- isAcroFormPresent: *boolean*
- isXFAPresent: *boolean*
- isCollectionPresent: *boolean*
- userInfo: *object*
- pageCount: *number*
- permissions: *[Permissions](#permissions)*
- toRaw(): *object*

<a id="version"></a>

### [Version](../parser-core/src/Version.js)

- major: *number*
- minor: *number*
- patch: *number*
- toString(): *string*

<a id="outlineItem"></a>

### [OutlineItem](./src/model/OutlineItem.js)

- dest: *?string|\*[]*
- url: *?string*
- title: *string*
- color: *[Color](#color)*
- bold: *boolean*
- italic: *boolean*
- depth: *number* (**Default: 0**)
- children: *[OutlineItem](#outlineItem)[]*
- page: *?number*
- toRaw(): *object*

<a id="color"></a>

### [Color](../src/model/Color.js)

- red: *number*
- green: *number*
- blue: *number*
- intValue: *number* (ex: `7237488`)
- hexString: *string* (ex: `'#6e6f70'`)
- rgbString: *string* (ex: `'rgb(110, 111, 112)'`)
- toRaw(): *object*

<a id="permissions"></a>

### [Permissions](../src/model/Permissions.js)

- allowPrinting: *boolean*
- allowContentsModifying: *boolean*
- allowCopying: *boolean*
- allowAnnotationsModifying: *boolean*
- allowInteractiveFormsModifying: *boolean*
- allowCopyingForAccessibility: *boolean*
- allowAssembling: *boolean*
- allowHighQualityPrinting: *boolean*
- toRaw(): *?number[]*

<a id="parseOptions"></a>

## Parse Options

* [fakeWorker](#fakeWorker)

---

<a id="fakeWorker"></a>

### fakeWorker: *`boolean`*

Use fake worker when used in a browser environment such as Electron Renderer Proccess.

**Default:** `false`

## License

[Apache-2.0](https://github.com/ridi/content-parser/packages/pdf-parser/LICENSE)

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