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

I want to decrypt an encrypted pdf using its password #1721

Closed
MC-Spraint opened this issue Jan 21, 2025 · 4 comments
Closed

I want to decrypt an encrypted pdf using its password #1721

MC-Spraint opened this issue Jan 21, 2025 · 4 comments
Labels

Comments

@MC-Spraint
Copy link

MC-Spraint commented Jan 21, 2025

When I try to decrypt an encrypted pdf, providing its password, I encounter the following error-
Input document to PDFDocument.load is encrypted. You can use PDFDocument.load(..., { ignoreEncryption: true }) if you wish to load the document anyways.

The thing is that I don't want to set ignoreEncryption to true.

Please help, this is the code :

import fs from 'fs';
import { PDFDocument } from 'pdf-lib';

async function decryptPdf() {
try {
const inputFile = './encrypted.pdf'; // Path to the encrypted PDF
const password = '2507'; // Password to decrypt
const outputFile = './decrypted.pdf'; // Path to save the decrypted PDF

    // Read the encrypted PDF
    const encryptedPdfBytes = fs.readFileSync(inputFile);

    // Load the PDF with the password
    const pdfDoc = await PDFDocument.load(encryptedPdfBytes, { password });

    // Save the decrypted PDF
    const decryptedPdfBytes = await pdfDoc.save();
    fs.writeFileSync(outputFile, decryptedPdfBytes);

    console.log(`Decryption successful! File saved at: ${outputFile}`);
} catch (error) {
    if (error.message.includes('Invalid Password')) {
        console.error('Decryption failed: Invalid password.');
    } else {
        console.error(`An error occurred: ${error.message}`);
    }
}

}

decryptPdf();

@Sharcoux
Copy link

Please, read the Issue section. Read the first Issue below your current issue. Also read the existing issues related to your problem.

@Sharcoux
Copy link

And close your issue so other users can see the problem better.

Copy link

github-actions bot commented Feb 6, 2025

This issue is stale because it has been open 2 weeks with no activity. It will be closed in 2 days unless there is new activity. See MAINTAINERSHIP.md#issues for details.

@github-actions github-actions bot added the stale label Feb 6, 2025
Copy link

github-actions bot commented Feb 9, 2025

This issue was closed because it went 2 weeks with no activity. Feel free to leave a comment if you feel it should be reopened!

@github-actions github-actions bot closed this as completed Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants