Skip to content

Commit

Permalink
feat(#72): provide a basic command line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Dec 27, 2023
1 parent 5623a60 commit 4257fd9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ This library is inspired by the [PHP library PDFMerger](https://github.com/myoky

`npm install --save pdf-merger-js`

of global installation if you want to use the cli tool:

`npm install -g pdf-merger-js`

## Usage

### CLI

`pdf-merge --output ./merged.pdf ./input1.pdf#1-2 ./input2.pdf#1,2,5-7`

### node.js

The node.js version has the following export functions:
Expand Down
46 changes: 46 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env node

import { program } from 'commander'

import PDFMerger from './index.js'
import { parsePagesString } from './parsePagesString.js'

import packageJson from './package.json' assert { type: 'json' };

async function main() {
program
.version(packageJson.version)
.description(packageJson.description)
.option('-o, --output <outputFile>', 'Merged PDF output file path')
.arguments('<inputFiles...>')
.action(async (inputFiles, cmd) => {
const outputFile = cmd.output;

if (!outputFile) {
console.error('Please provide an output file using the --output flag');
return;
}

try {
await mergePDFs(outputFile, inputFiles);
console.log(`Merged PDFs successfully into ${outputFile}`);
} catch (error) {
console.error('An error occurred while merging PDFs:', error);
}
});

program.parse(process.argv);
}

async function mergePDFs(outputFile, inputFiles) {
const merger = new PDFMerger();

for (const inputFile of inputFiles) {
const [filePath, pageRange] = inputFile.split('#');
const pages = pageRange ? parsePagesString(pageRange) : null;
await merger.add(filePath, pages);
}
await merger.save(outputFile);
}

main();
17 changes: 17 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"main": "./index.js",
"types": "./index.d.ts",
"browser": "./browser.js",
"bin": {
"pdf-merge": "cli.js"
},
"scripts": {
"standard": "standard",
"standard:fix": "standard --fix",
Expand All @@ -29,6 +32,7 @@
"author": "nbesli",
"license": "MIT",
"dependencies": {
"commander": "^11.1.0",
"pdf-lib": "^1.17.1"
},
"devDependencies": {
Expand Down
41 changes: 41 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'path'
import fs from 'fs-extra'
import pdfDiff from 'pdf-diff'
import { exec } from 'child_process'
import util from 'util'
import { jest } from '@jest/globals'

const asyncExec = util.promisify(exec);

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

jest.setTimeout(10000)

async function mergePDFsCli(outputFile, inputFiles) {
const { stdout, stderr } = await asyncExec(`node ./cli.js --output ${outputFile} ${inputFiles.join(' ')}`)
return { stdout, stderr }
}

describe('issues', () => {
beforeAll(async () => {
await fs.ensureDir(TMP_DIR)
})

test('should merge two pdfs', async () => {
await mergePDFsCli(path.join(TMP_DIR, 'Testfile_AB.pdf'), [
path.join(FIXTURES_DIR, 'Testfile_A.pdf'),
path.join(FIXTURES_DIR, 'Testfile_B.pdf')
])
const diff1 = await pdfDiff(
path.join(FIXTURES_DIR, 'Testfile_AB.pdf'),
path.join(TMP_DIR, 'Testfile_AB.pdf')
)
expect(diff1).toBeFalsy()
});

afterAll(async () => {
await fs.remove(TMP_DIR)
})
})

0 comments on commit 4257fd9

Please sign in to comment.