Skip to content
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
2 changes: 1 addition & 1 deletion packages/pieces/community/pdf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@activepieces/piece-pdf",
"version": "0.2.17",
"version": "0.2.18",
"dependencies": {
"jimp": "^0.22.12",
"pdf-parse": "1.1.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/pieces/community/pdf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { imageToPdf } from './lib/actions/image-to-pdf';
import { pdfPageCount } from './lib/actions/pdf-page-count';
import { extractPdfPages } from './lib/actions/extract-pdf-pages';
import { mergePdfs } from './lib/actions/merge-pdfs';
import { removePassword } from './lib/actions/remove-password';

export const PDF = createPiece({
displayName: 'PDF',
Expand All @@ -27,6 +28,7 @@ export const PDF = createPiece({
pdfPageCount,
extractPdfPages,
mergePdfs,
removePassword,
],
triggers: [],
});
67 changes: 67 additions & 0 deletions packages/pieces/community/pdf/src/lib/actions/remove-password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { PDFDocument } from 'pdf-lib';

export const removePassword = createAction({
name: 'removePassword',
displayName: 'Remove Password',
description: 'Remove password protection from a PDF file to enable content manipulation',
props: {
file: Property.File({
displayName: 'PDF File',
description: 'Password-protected PDF file',
required: true,
}),
password: Property.ShortText({
displayName: 'Password',
description: 'Password to unlock the PDF',
required: true,
}),
outputFileName: Property.ShortText({
displayName: 'Output File Name',
description: 'Name for the unprotected PDF file (without extension)',
required: false,
defaultValue: 'unlocked-document',
}),
},
errorHandlingOptions: {
continueOnFailure: {
defaultValue: false,
},
retryOnFailure: {
hide: true,
},
},
async run(context) {
const { file, password, outputFileName } = context.propsValue;

try {
const pdfBytes = new Uint8Array(Buffer.isBuffer(file.data) ? file.data : Buffer.from(file.data));

const pdfDoc = await PDFDocument.load(pdfBytes, {
password,
ignoreEncryption: true,
updateMetadata: false,
} as any);

const decryptedBytes = await pdfDoc.save();

return context.files.write({
data: Buffer.from(decryptedBytes),
fileName: `${outputFileName}.pdf`,
});

} catch (error) {
const errorMessage = (error as Error).message || 'Unknown error';

if (errorMessage.includes('password')) {
throw new Error('Invalid password or PDF is not password-protected');
}
if (errorMessage.includes('encrypted') || errorMessage.includes('encryption')) {
throw new Error('Unsupported encryption type.');
}

throw new Error(`Failed to remove password from PDF: ${errorMessage}`);
}
},
});