DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.36k stars 595 forks source link

Protect a PDF from modification #1595

Open carpentierchloe opened 5 months ago

carpentierchloe commented 5 months ago

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

DavBfr commented 5 months ago

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.

carpentierchloe commented 5 months ago

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

DavBfr commented 5 months ago

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());
carpentierchloe commented 4 months ago

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

DavBfr commented 4 months ago

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