Skip to content

Commit aa1f4ae

Browse files
committed
Add a password option
1 parent 92b5570 commit aa1f4ae

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cantoo/pdf-lib",
3-
"version": "1.18.0",
3+
"version": "1.19.0",
44
"description": "Create and modify PDF files with JavaScript",
55
"author": "Andrew Dillon <andrew.dillon.j@gmail.com>",
66
"contributors": [

src/api/PDFDocument.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export default class PDFDocument {
135135
throwOnInvalidObject = false,
136136
updateMetadata = true,
137137
capNumbers = false,
138+
password
138139
} = options;
139140

140141
assertIs(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
@@ -160,7 +161,7 @@ export default class PDFDocument {
160161
capNumbers,
161162
pdfDoc.cryptoFactory
162163
).parseDocument();
163-
return new PDFDocument(context, true, updateMetadata, true);
164+
return new PDFDocument(context, true, updateMetadata, true, password);
164165
}
165166
return pdfDoc;
166167
}
@@ -210,7 +211,8 @@ export default class PDFDocument {
210211
context: PDFContext,
211212
ignoreEncryption: boolean,
212213
updateMetadata: boolean,
213-
isDecrypted: boolean
214+
isDecrypted: boolean,
215+
password?: string
214216
) {
215217
assertIs(context, 'context', [[PDFContext, 'PDFContext']]);
216218
assertIs(ignoreEncryption, 'ignoreEncryption', ['boolean']);
@@ -222,7 +224,7 @@ export default class PDFDocument {
222224
if (this.isEncrypted && !isDecrypted) {
223225
const encryptDict = context.lookup(context.trailerInfo.Encrypt, PDFDict);
224226
const fileIds = context.lookup(context.trailerInfo.ID, PDFArray);
225-
this.cryptoFactory = new CipherTransformFactory(encryptDict, (fileIds.get(0) as PDFHexString).asBytes())
227+
this.cryptoFactory = new CipherTransformFactory(encryptDict, (fileIds.get(0) as PDFHexString).asBytes(), password)
226228
} else if (this.isEncrypted) {
227229
// context.delete(context.trailerInfo.Encrypt);
228230
delete context.trailerInfo.Encrypt;

src/api/PDFDocumentOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface LoadOptions {
2727
throwOnInvalidObject?: boolean;
2828
updateMetadata?: boolean;
2929
capNumbers?: boolean;
30+
password?: string
3031
}
3132

3233
export interface CreateOptions {

0 commit comments

Comments
 (0)