Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pdfjs and convert to pdfExtract esm #52

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ name: test

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 13.x, 14.x, 15.x, 16.x, 17.x]
node-version:
[12.x, 13.x, 14.x, 15.x, 16.x, 17.x, 18.x, 19.x, 20.x, 21.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
env:
CI: true
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
env:
CI: true
62 changes: 46 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,44 @@ Note: NO OCR!
[![NPM](https://nodei.co/npm/pdf.js-extract.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/pdf.js-extract/)

![test](https://github.com/ffalt/pdf.js-extract/workflows/test/badge.svg)
[![license](https://img.shields.io/npm/l/pdf.js-extract.svg)](http://opensource.org/licenses/MIT)
[![license](https://img.shields.io/npm/l/pdf.js-extract.svg)](http://opensource.org/licenses/MIT)

## Example Usage

### Esm version >= 1.0.0

javascript async with promise

```javascript
import { PDFExtract } from "pdf.js-extract";
const pdfExtract = new PDFExtract();
const options = {}; /* see below */
pdfExtract
.extract("example.pdf", options)
.then((data) => console.log(data))
.catch((err) => console.log(err));
```

### Commonjs version < 1.0.0

javascript async with callback

```javascript
const PDFExtract = require('pdf.js-extract').PDFExtract;
const PDFExtract = require("pdf.js-extract").PDFExtract;
const pdfExtract = new PDFExtract();
const options = {}; /* see below */
pdfExtract.extract('test.pdf', options, (err, data) => {
pdfExtract.extract("example.pdf", options, (err, data) => {
if (err) return console.log(err);
console.log(data);
});
```

javascript async with callback using buffer

```javascript
const PDFExtract = require('pdf.js-extract').PDFExtract;
const PDFExtract = require("pdf.js-extract").PDFExtract;
const pdfExtract = new PDFExtract();
const fs = require('fs');
const fs = require("fs");
const buffer = fs.readFileSync("./example.pdf");
const options = {}; /* see below */
pdfExtract.extractBuffer(buffer, options, (err, data) => {
Expand All @@ -43,17 +61,22 @@ pdfExtract.extractBuffer(buffer, options, (err, data) => {
});
```

### Typescript

typescript async with promise

```typescript
import {PDFExtract, PDFExtractOptions} from 'pdf.js-extract';
import { PDFExtract, PDFExtractOptions } from "pdf.js-extract";
const pdfExtract = new PDFExtract();
const options: PDFExtractOptions = {}; /* see below */
pdfExtract.extract('test.pdf', options)
.then(data => console.log(data))
.catch(err=> console.log(err));
pdfExtract
.extract("example.pdf", options)
.then((data) => console.log(data))
.catch((err) => console.log(err));
```

## Options

```typescript
export interface PDFExtractOptions {
firstPage?: number; // default:`1` - start extract at page nr
Expand All @@ -73,14 +96,23 @@ Example Output
"meta": {
"info": {
"PDFFormatVersion": "1.7",
"Language": null,
"EncryptFilterName": null,
"IsLinearized": false,
"IsAcroFormPresent": false,
"IsXFAPresent": false,
"IsCollectionPresent": false,
"IsLinearized": true,
"IsXFAPresent": false
"IsSignaturesPresent": false,
"Author": "someone",
"CreationDate": "D:20000629102108+11'00'",
"Creator": "Microsoft Word 8.0",
"ModDate": "D:20131028152413-04'00'",
"Producer": "Acrobat Distiller 4.0 for Windows",
"Title": "This is a test PDF file"
},
"metadata": {
"dc:format": "application/pdf",
"dc:creator": "someone",
"dc:creator": ["someone"],
"dc:title": "This is a hello world PDF file",
"xmp:createdate": "2000-06-29T10:21:08+11:00",
"xmp:creatortool": "Microsoft Word 8.0",
Expand All @@ -102,9 +134,7 @@ Example Output
"width": 200,
"height": 200
},
"links": [
"https://github.com"
],
"links": ["https://github.com"],
"content": [
{
"x": 70,
Expand All @@ -113,7 +143,7 @@ Example Output
"dir": "ltr",
"width": 64.656,
"height": 12,
"fontName": "Times"
"fontName": "g_d0_f1"
}
]
}
Expand Down
39 changes: 14 additions & 25 deletions example/encrypted-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
"meta": {
"info": {
"PDFFormatVersion": "1.7",
"IsAcroFormPresent": false,
"IsCollectionPresent": false,
"Language": "en-GB",
"EncryptFilterName": "Standard",
"IsLinearized": false,
"IsAcroFormPresent": false,
"IsXFAPresent": false,
"IsCollectionPresent": false,
"IsSignaturesPresent": false,
"Language": "en-GB",
"Author": "Ilan Copelyn\u0000",
"Creator": "Microsoft® Word 2016\u0000",
"EncryptFilterName": "Standard",
"Producer": "Microsoft® Word 2016\u0000",
"CreationDate": "D:20180201124627+02'00'",
"ModDate": "D:20180201124627+02'00'"
"ModDate": "D:20180201124627+02'00'",
"Producer": "Microsoft® Word 2016\u0000"
},
"metadata": {
"pdf:producer": "Microsoft® Word 2016",
"dc:creator": ["Ilan Copelyn"],
"dc:creator": [
"Ilan Copelyn"
],
"xmp:creatortool": "Microsoft® Word 2016",
"xmp:createdate": "2018-02-01T12:46:27+02:00",
"xmp:modifydate": "2018-02-01T12:46:27+02:00",
Expand All @@ -43,7 +45,7 @@
"y": 82.43999999999994,
"str": "Hello I",
"dir": "ltr",
"width": 28.549439999999997,
"width": 28.549440000000004,
"height": 11.04,
"fontName": "g_d0_f1"
},
Expand All @@ -61,7 +63,7 @@
"y": 82.43999999999994,
"str": "m an e",
"dir": "ltr",
"width": 30.326880000000003,
"width": 30.326879999999996,
"height": 11.04,
"fontName": "g_d0_f1"
},
Expand All @@ -70,7 +72,7 @@
"y": 82.43999999999994,
"str": "ncr",
"dir": "ltr",
"width": 14.285759999999998,
"width": 14.285760000000002,
"height": 11.04,
"fontName": "g_d0_f1"
},
Expand All @@ -79,25 +81,12 @@
"y": 82.43999999999994,
"str": "ypted pdf",
"dir": "ltr",
"width": 43.07807999999999,
"height": 11.04,
"fontName": "g_d0_f1"
},
{
"x": 190.85,
"y": 82.43999999999994,
"str": " ",
"dir": "ltr",
"width": 2.49504,
"width": 43.07807999999997,
"height": 11.04,
"fontName": "g_d0_f1"
}
]
}
],
"pdfInfo": {
"numPages": 1,
"fingerprint": "2e22bde07d96d0408524c26eeecd3483"
},
"filename": "./encrypted.pdf"
}
}
2 changes: 1 addition & 1 deletion example/encrypted-output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello I’m an encrypted pdf
Hello I’m an encrypted pdf
20 changes: 0 additions & 20 deletions example/encrypted.js

This file was deleted.

27 changes: 27 additions & 0 deletions example/encrypted.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { writeFileSync } from "fs";
import { PDFExtract } from "../lib/index.mjs";

const pdfExtract = new PDFExtract();
pdfExtract.extract(
"./encrypted.pdf",
{ password: "password" },
function (err, data) {
if (err) {
// eslint-disable-next-line no-console
return console.error(err);
}

writeFileSync("./encrypted-output.json", JSON.stringify(data, null, "\t"));
// eslint-disable-next-line no-console
console.log(JSON.stringify(data, null, "\t"));

const lines = PDFExtract.utils.pageToLines(data.pages[0], 2);
const rows = PDFExtract.utils.extractTextRows(lines);
const text = rows
.map(function (row) {
return row.join("");
})
.join("\n");
writeFileSync("./encrypted-output.txt", text);
},
);
11 changes: 0 additions & 11 deletions example/example-async.js

This file was deleted.

13 changes: 13 additions & 0 deletions example/example-async.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PDFExtract } from "../lib/index.mjs";

async function run() {
const pdfExtract = new PDFExtract();
const data = await pdfExtract.extract("./example.pdf", {} /* options*/);
// eslint-disable-next-line no-console
console.log(JSON.stringify(data, null, "\t"));
}

run().catch((e) => {
// eslint-disable-next-line no-console
console.error(e);
});
13 changes: 0 additions & 13 deletions example/example-buffer.js

This file was deleted.

18 changes: 18 additions & 0 deletions example/example-buffer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFileSync } from "fs";
import { deepEqual } from "assert";
import { PDFExtract } from "../lib/index.mjs";

const pdfExtract = new PDFExtract();

const json = JSON.parse(readFileSync("./example-output.json", "utf8"));
const buffer = readFileSync("./example.pdf");

pdfExtract.extractBuffer(buffer, {}, (err, data) => {
if (err) {
// eslint-disable-next-line no-console
return console.error(err);
}

deepEqual(data.meta, json.meta);
deepEqual(data.pages, json.pages);
});
10 changes: 5 additions & 5 deletions example/example-cmap-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"meta": {
"info": {
"PDFFormatVersion": "1.3",
"Language": null,
"EncryptFilterName": null,
"IsLinearized": false,
"IsAcroFormPresent": false,
"IsCollectionPresent": false,
"IsXFAPresent": false,
"IsCollectionPresent": false,
"IsSignaturesPresent": false,
"EncryptFilterName": null,
"Language": null,
"Producer": "FPDF 1.52",
"CreationDate": "D:20130729015932"
},
Expand All @@ -34,10 +34,10 @@
"dir": "ltr",
"width": 140,
"height": 20,
"fontName": "ºÚÌå"
"fontName": "g_d0_f1"
}
]
}
],
"filename": "./example-cmap.pdf"
}
}
Loading