Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Flutter Guides Certificate management

Certificate Management ​

Starting with 3.2.0, ComPDF Flutter SDK supports PKCS#12 certificates (.p12, .pfx) for digital signing. Certificate APIs are called through ComPDFKit and do not require an opened PDF.

dart
import 'package:compdfkit_flutter/compdfkit.dart';

Generate a Certificate ​

Use CPDFCertificateRequest to specify the subject, password, and output path. The output directory must exist and be writable.

dart
final directory = await ComPDFKit.getTemporaryDirectory();
final certificatePath = '${directory.path}/signer.p12';
// Obtain certificatePassword from user input in your application.
final created = await ComPDFKit.generatePkcs12Certificate(
  CPDFCertificateRequest(
    subject: const CPDFCertificateSubject(
      commonName: 'Document Signer',
      organization: 'Example Organization',
    ),
    password: certificatePassword,
    outputPath: certificatePath,
    usage: CPDFCertificateUsage.digitalSignature,
  ),
);

commonName is required. You can also set the country, state, locality, organizational unit, and email address. Generating a certificate does not establish trust on the recipient's system.

Check the Password and Read Information ​

dart
final validPassword = await ComPDFKit.checkPkcs12CertificatePassword(
  certificatePath,
  certificatePassword,
);
if (validPassword) {
  final certificate = await ComPDFKit.loadPkcs12Certificate(
    certificatePath,
    certificatePassword,
  );
  if (certificate != null) {
    print(certificate.subject);
    print(certificate.issuer);
    print(certificate.validTo);
    print(certificate.isTrusted);
  }
}

loadPkcs12Certificate() returns the subject, issuer, validity period, algorithms, and digests. It returns null if the certificate cannot be loaded. Calls may also throw native platform exceptions; provide failure feedback in your application.

Add a Trusted Certificate ​

dart
final trusted = await ComPDFKit.addTrustedCertificate(
  certificatePath,
  certificatePassword,
);
if (trusted) {
  final certificate = await ComPDFKit.loadPkcs12Certificate(
    certificatePath,
    certificatePassword,
  );
  print(certificate?.isTrusted);
}

The trust state belongs to the current application's certificate store. It does not imply trust in other applications or on a recipient's system. Reload the certificate after adding it to read the updated state.

A certificate file contains a private key and should be stored securely. Do not log certificate passwords or store them in document metadata. To sign a PDF, see Create Digital Signatures.