pdf_crypto

Pdf encryption library for dart / flutter

Generate an encrypted document

final pdf = Document();

// Declare the encryption options
pdf.document.encryption = PdfEncryptionAES(
  pdf.document,
  user: '1234', // The user password (can copy and print)
  owner: '5678', // The owner password (has all rights)
  accessFlags: <PdfAccessFlags>{
    PdfAccessFlags.Copy,
    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());

Generate a signed document

final pdf = Document();

// Convert the PEM private key to BER
final privateKey = PdfSign.pemPrivateKey(
  File('key0.pem').readAsStringSync(),
);

// Convert the PEM x509 certificate to BER
final certificate = PdfSign.pemCertificate(
  File('cert0.pem').readAsStringSync(),
);

// If the chain is made of multiple certificates and a CA,
// put the sign certificate first.
final chain = [certificate];

pdf.addPage(
  Page(
    build: (Context context) => Column(
      children: <Widget>[
        Text('Click on this signature for more info:'),
        Signature(
          name: 'Signature',
          value: PdfSign.rsaSha1(
            privateKey: privateKey,
            certificates: chain,
          ),
          child: PdfLogo(),
        ),
      ],
    ),
  ),
);

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

Generate a self-signed certificate

Generate a 2048 bits RSA key pair using OpenSSL

openssl genpkey -outform PEM -algorithm rsa -pkeyopt rsa_keygen_bits:2048 -out key.pem

Generate a self-signed certificate using OpenSSL for tests

openssl req -new -x509 -days 365 -key key.pem -out cert.pem -subj "/CN=John Doe"

Load an existing document for updating

It’s also possible to include a signature to the existing document.

// Load the existing document from disk (can be from network or anything else)
final template = File('template.pdf').readAsBytesSync();

// Import the original document and draw something on the first page
final pdf = Document.load(
  PdfDocumentParser(template),
);

// Update the first page
pdf.editPage(
  0, // index 0 for the first page
  Page(
    build: (context) => Center(
      child: Text('Hello World!'),
    ),
  ),
);

// Save the document to a new file
final file = File('updated.pdf');
await file.writeAsBytes(await pdf.save());

Merge multiple documents

final tools = PdfTools();

final parsed1 = PdfDocumentParser(await File('doc1.pdf').readAsBytes());
tools.addPages(parsed1);

final parsed2 = PdfDocumentParser(await File('doc2.pdf').readAsBytes());
tools.addPages(parsed2, [0, 2]); // only pages 0 and 2

final result = tools.save();

final file = File('merged.pdf');
await file.writeAsBytes(result);

Buy now

The premium version of the PDF package for Flutter is updated with the main package, to keep compatibility, and includes:

For this, you get a perpetual license for any number of applications and developers, but not the right to share or sell the source code.

The process is not automated, so if you buy while I’m not available, you’ll not get the information from me in the minute. Usually you’ll get it within 6 hours.

Use this package as a library

  1. Depend on it

    Add this to your package’s pubspec.yaml file, replace PASSWORD with your credentials for this library:

    dependencies:
      pdf_crypto:
        hosted:
          name: pdf_crypto
          url: https://pub.nfet.net/PASSWORD
        version: ^3.3.5
  2. Install it

    You can install packages from the command line:

    with Flutter:

    $ flutter pub get

    Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

  3. Import it

    Now in your Dart code, you can use:

    import 'package:pdf_crypto/pdf_crypto.dart';

Changelog

3.3.4

3.3.3

3.3.2

3.3.1

3.3.0

3.2.9

3.2.8

3.2.7

3.2.6

3.2.5

3.2.4

3.2.3

3.2.2

3.2.1

3.2.0

3.1.0

3.0.2

3.0.1

3.0.0

2.0.1

2.0.0

1.0.5

1.0.4

1.0.3

1.0.2

1.0.1

1.0.0

Documentation

API reference


  1. Only for new documents created from code.↩︎