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

switch from CommonJS to ESM #128

Merged
merged 3 commits into from
Dec 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
48 changes: 21 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Description

This node.js library can **merge multiple PDF documents**, or parts of them, to one new PDF document. It's only dependency is [pdf-lib](https://pdf-lib.js.org/) so it can run in any javascript-only environment **without any external dependencies**.
This node.js library can **merge multiple PDF documents**, or parts of them, to one new PDF document. It's only dependency is [pdf-lib](https://pdf-lib.js.org/) so it can run in any javascript-only environment **without any non-javascript dependencies**.

> If you are searching for the legacy version based on
## Legacy notes

* If you are searching for the legacy version based on
[pdfjs](https://www.npmjs.com/package/pdfjs) please install a [v3 release](https://github.com/nbesli/pdf-merger-js/releases?q=v3&expanded=true). Since [v4](https://github.com/nbesli/pdf-merger-js/releases?q=v4&expanded=true) we use [pdf-lib](https://pdf-lib.js.org/) instead.
* If you are searching for a legacy version using CommonJS modules please install a [v4 release](https://github.com/nbesli/pdf-merger-js/releases?q=v4&expanded=true). Since [v5](https://github.com/nbesli/pdf-merger-js/releases?q=v5&expanded=true) we use the modern ESM ("import") instead of the CommonJS ("require) module standard.

This library is inspired by the [PHP library PDFMerger](https://github.com/myokyawhtun/PDFMerger) and has a very similar API.

Expand All @@ -22,10 +25,10 @@ The node.js version has the following export functions:
* `setMetadata` set Metadata for producer, author, title or creator.
* `reset` resets the internal state of the document, to start again.

### async node.js example
#### async node.js example

```js
const PDFMerger = require('pdf-merger-js');
import PDFMerger from 'pdf-merger-js';

var merger = new PDFMerger();

Expand All @@ -37,6 +40,14 @@ var merger = new PDFMerger();
await merger.add('pdf3.pdf', '3 to 5'); //merge pages 3 to 5 (3,4,5)
await merger.add('pdf3.pdf', '3-5'); //merge pages 3 to 5 (3,4,5)

// Set metadata
await merger.setMetadata({
producer: "pdf-merger-js based script",
author: "John Doe",
creator: "John Doe",
title: "My live as John Doe"
});

await merger.save('merged.pdf'); //save under given name and reset the internal document

// Export the merged PDF as a nodejs Buffer
Expand All @@ -55,7 +66,7 @@ The Browser version has the following export functions:
* `setMetadata` set Metadata for producer, author, title or creator.
* `reset` resets the internal state of the document, to start again.

#### Sample - React
#### async react example

```jsx
import PDFMerger from 'pdf-merger-js/browser';
Expand All @@ -70,9 +81,13 @@ const Merger = (files) => {
const merger = new PDFMerger();

for(const file of files) {
await merger.add(file)
await merger.add(file);
}

await merger.setMetadata({
producer: "pdf-merger-js based script"
});

const mergedPdf = await merger.saveAsBlob();
const url = URL.createObjectURL(mergedPdf);

Expand All @@ -99,27 +114,6 @@ const Merger = (files) => {
};
```

#### Sample - Set Metadata

```js
const merger = new PDFMerger();

// Add files

// Set only producer
await merger.setMetadata({
producer: "Custom Producer",
});

// Set all 4 fields
await merger.setMetadata({
producer: "Custom Producer",
author: "Custom Author",
creator: "Custom Creator",
title: "Custom Title"
});
```

## Similar libraries

* [pdf-merge](https://www.npmjs.com/package/pdf-merge) has a dependency on [PDFtk](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/).
Expand Down
6 changes: 2 additions & 4 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { PDFDocument } = require('pdf-lib')
import { PDFDocument } from 'pdf-lib'

const globalObject =
typeof globalThis === 'object'
Expand All @@ -9,7 +9,7 @@ const globalObject =
? self // Worker
: this

class PDFMerger {
export default class PDFMerger {
constructor () {
this.reset()

Expand Down Expand Up @@ -167,5 +167,3 @@ class PDFMerger {
link.click()
}
}

module.exports = PDFMerger
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { PDFDocument } = require('pdf-lib')
const fs = require('fs').promises
import { PDFDocument } from 'pdf-lib'
import fs from 'fs/promises'

class PDFMerger {
export default class PDFMerger {
constructor () {
this.reset()

Expand Down Expand Up @@ -122,5 +122,3 @@ class PDFMerger {
return Buffer.from(uInt8Array)
}
}

module.exports = PDFMerger
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
"name": "pdf-merger-js",
"version": "4.3.1",
"description": "merge multiple PDF documents, or parts of them, to a new PDF document",
"type": "module",
"engines": {
"node": ">=14"
},
"main": "./index.js",
"types": "./index.d.ts",
"browser": "./browser.js",
"scripts": {
"standard": "standard",
"standard:fix": "standard --fix",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch --forceExit --detectOpenHandles"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest --detectOpenHandles",
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --forceExit --detectOpenHandles"
},
"standard": {
"env": [
Expand Down
15 changes: 9 additions & 6 deletions test/browser-fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
* @jest-environment jsdom
*/

const path = require('path')
const fs = require('fs-extra')
const pdfDiff = require('pdf-diff')
const fetch = require('node-fetch')
import path from 'path'
import fs from 'fs-extra'
import pdfDiff from 'pdf-diff'
import fetch from 'node-fetch'
import { jest } from '@jest/globals'

import PDFMerger from '../browser'

/*
add a global `windows.fetch` to mock fetch
*/
global.window.fetch = jest.fn().mockImplementation((requestUrl) => {
return Promise.resolve(fetch(requestUrl))
})

const PDFMerger = require('../browser')

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down
12 changes: 7 additions & 5 deletions test/fixtures.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const path = require('path')
const fs = require('fs-extra')
const pdfDiff = require('pdf-diff')
const { PDFDocument } = require('pdf-lib')
import path from 'path'
import fs from 'fs-extra'
import pdfDiff from 'pdf-diff'
import { PDFDocument } from 'pdf-lib'
import { jest } from '@jest/globals'

const PDFMerger = require('../index')
import PDFMerger from '../index'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down
8 changes: 5 additions & 3 deletions test/issues.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const path = require('path')
const fs = require('fs-extra')
import path from 'path'
import fs from 'fs-extra'
import { jest } from '@jest/globals'

const PDFMerger = require('../index')
import PDFMerger from '../index'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const FIXTURES_DIR = path.join(__dirname, 'fixtures')
const TMP_DIR = path.join(__dirname, 'tmp')

Expand Down
Loading