final pdf = Document();
// Declare the encryption options
.document.encryption = PdfEncryptionAES(
pdf.document,
pdf: '1234', // The user password (can copy and print)
user: '5678', // The owner password (has all rights)
owner: <PdfAccessFlags>{
accessFlags.Copy,
PdfAccessFlags.Print,
PdfAccessFlags},
: PdfAESLevel.high,
level
);
.addPage(
pdf
Page(: (Context context) {
buildreturn Text('This text is encrypted using AES 256');
},
,
)
);
final File file = File('encrypted.pdf');
await file.writeAsBytes(await pdf.save());
final pdf = Document();
// Convert the PEM private key to BER
final privateKey = PdfSign.pemPrivateKey(
'key0.pem').readAsStringSync(),
File(
);
// Convert the PEM x509 certificate to BER
final certificate = PdfSign.pemCertificate(
'cert0.pem').readAsStringSync(),
File(
);
// If the chain is made of multiple certificates and a CA,
// put the sign certificate first.
final chain = [certificate];
.addPage(
pdf
Page(: (Context context) => Column(
build: <Widget>[
children'Click on this signature for more info:'),
Text(
Signature(: 'Signature',
name: PdfSign.rsaSha1(
value: privateKey,
privateKey: chain,
certificates,
): PdfLogo(),
child,
),
],
),
)
);
final File file = File('signed.pdf');
await file.writeAsBytes(await pdf.save());
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"
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
.editPage(
pdf0, // index 0 for the first page
Page(: (context) => Center(
build: Text('Hello World!'),
child,
),
)
);
// Save the document to a new file
final file = File('updated.pdf');
await file.writeAsBytes(await pdf.save());
final tools = PdfTools();
final parsed1 = PdfDocumentParser(await File('doc1.pdf').readAsBytes());
.addPages(parsed1);
tools
final parsed2 = PdfDocumentParser(await File('doc2.pdf').readAsBytes());
.addPages(parsed2, [0, 2]); // only pages 0 and 2
tools
final result = tools.save();
final file = File('merged.pdf');
await file.writeAsBytes(result);
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.
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.9
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.
Import it
Now in your Dart code, you can use:
import 'package:pdf_crypto/pdf_crypto.dart';
Only for new documents created from code.↩︎