Skip to content

Instantly share code, notes, and snippets.

@DavBfr
Created March 25, 2022 12:27
Show Gist options
  • Save DavBfr/4a5bc76b23761033246d48558ddbad1b to your computer and use it in GitHub Desktop.
Save DavBfr/4a5bc76b23761033246d48558ddbad1b to your computer and use it in GitHub Desktop.
Pdf Digital signature
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:pdf_crypto/pdf_crypto.dart';
import 'package:printing/printing.dart';
Future<void> main() async {
runApp(const MyApp('Printing Demo'));
}
class MyApp extends StatelessWidget {
const MyApp(this.title, {Key? key}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text(title)),
body: PdfPreview(
build: (format) => _generatePdf(format, title),
),
),
);
}
Future<Uint8List> _generatePdf(PdfPageFormat format, String title) async {
final pdf = pw.Document(version: PdfVersion.pdf_1_5, compress: true);
final font = await PdfGoogleFonts.nunitoExtraLight();
pdf.addPage(
pw.Page(
pageFormat: format,
build: (context) {
return pw.Column(
children: [
pw.SizedBox(
width: double.infinity,
child: pw.FittedBox(
child: pw.Text(title, style: pw.TextStyle(font: font)),
),
),
pw.SizedBox(height: 20),
pw.Flexible(child: pw.FlutterLogo()),
pw.Signature(
name: 'Signature',
value: PdfSign.pkcs7Rsa(
permissions: PdfSignPerms.none,
digest: HashAlgorithm.sha512,
privateKey: PdfSign.pemPrivateKey(
privateKey,
)!,
issuer: X509.fromPem(certificate),
chain: [
X509.fromPem(ca),
],
),
child: pw.Container(
margin: const pw.EdgeInsets.all(20),
padding: const pw.EdgeInsets.all(10),
decoration: pw.BoxDecoration(
border: pw.Border.all(width: 2, color: PdfColors.amber),
borderRadius:
const pw.BorderRadius.all(pw.Radius.circular(10)),
),
child: pw.Text(
'Document Digitally Signed on ${DateTime.now()}',
),
),
),
],
);
},
),
);
return pdf.save();
}
static const privateKey = '''
-----BEGIN PRIVATE KEY-----
MIIEvQIBADAN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O5scsBkkTU9L1xViBOQ==
-----END CERTIFICATE-----
''';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment