dart_pdf icon indicating copy to clipboard operation
dart_pdf copied to clipboard

Protect a PDF from modification

Open carpentierchloe opened this issue 1 year ago • 5 comments

Is there a solution for preventing the user to modify the PDF ?

carpentierchloe avatar Feb 07 '24 15:02 carpentierchloe

Yes, you'll need the encryption library that can password-protect modifications. Note: That works only if the reader software is OK with implementing that restriction, like Acrobat Reader.

DavBfr avatar Feb 07 '24 15:02 DavBfr

Thank you, do you have an example of usage with a PDF ?

carpentierchloe avatar Feb 07 '24 15:02 carpentierchloe

Something like:

final pdf = Document();

// Declare the encryption options
pdf.document.encryption = PdfEncryptionAES(
  pdf.document,
  owner: 'MySecureSecretPassword', // The owner password (has all rights)
  accessFlags: <PdfAccessFlags>{
    PdfAccessFlags.Copy, // The user with no password can only copy/paste and print
    PdfAccessFlags.Print,
  },
  level: PdfAESLevel.high,
);

pdf.addPage(
  Page(
    build: (Context context) {
      return Text('This text is encrypted using AES 256');
    },
  ),
);

final File file = File('encrypted.pdf');
await file.writeAsBytes(await pdf.save());

DavBfr avatar Feb 07 '24 16:02 DavBfr

Which library are you using ? PdfEncryptionAES does not exist with library 'encrypt', and the library 'encrypt' does not manage pdf files...

carpentierchloe avatar Feb 12 '24 16:02 carpentierchloe

This library: https://pub.nfet.net/pdf_crypto/

DavBfr avatar Feb 12 '24 17:02 DavBfr