Skip to content

Commit

Permalink
fix(#88): add support for encrypted pdfs
Browse files Browse the repository at this point in the history
  • Loading branch information
mojoaxel committed Aug 25, 2022
1 parent a40cce6 commit da8993d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Expand Up @@ -4,6 +4,11 @@ const fs = require('fs').promises
class PDFMerger {
constructor () {
this.doc = undefined

this.loadOptions = {
// allow merging of encrypted pdfs (issue #88)
ignoreEncryption: true
}
}

async add (inputFile, pages) {
Expand Down Expand Up @@ -40,7 +45,7 @@ class PDFMerger {

async _addEntireDocument (input) {
const src = await this._getInputAsBuffer(input)
const srcDoc = await PDFDocument.load(src)
const srcDoc = await PDFDocument.load(src, this.loadOptions)

const copiedPages = await this.doc.copyPages(srcDoc, srcDoc.getPageIndices())
copiedPages.forEach((page) => {
Expand All @@ -57,7 +62,7 @@ class PDFMerger {
}

const src = await this._getInputAsBuffer(input)
const srcDoc = await PDFDocument.load(src)
const srcDoc = await PDFDocument.load(src, this.loadOptions)
const pageCount = srcDoc.getPageCount()

if (from >= pageCount || to >= pageCount) {
Expand All @@ -77,7 +82,7 @@ class PDFMerger {
}

const src = await this._getInputAsBuffer(input)
const srcDoc = await PDFDocument.load(src)
const srcDoc = await PDFDocument.load(src, this.loadOptions)

// switch from indexed 1 to indexed 0
const pagesIndexed1 = pages.map(p => p - 1)
Expand Down
Binary file added test/fixtures/issue-88.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions test/issues.test.js
Expand Up @@ -31,6 +31,12 @@ describe('issues', () => {
await merger.save(path.join(TMP_DIR, 'issue-42_merged.pdf'))
})

test('merge a encrypted pdf (#88)', async () => {
const merger = new PDFMerger()
await merger.add(path.join(FIXTURES_DIR, 'issue-88.pdf'))
await merger.save(path.join(TMP_DIR, 'issue-88_merged.pdf'))
})

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

0 comments on commit da8993d

Please sign in to comment.