generateDocument function
Future<Uint8List>
generateDocument(
- ScreeningResult result, [
- PdfPageFormat format = PdfPageFormat.a4
Implementation
Future<Uint8List> generateDocument(ScreeningResult result,
[PdfPageFormat format = PdfPageFormat.a4]) async {
final doc = pw.Document(pageMode: PdfPageMode.outlines);
final font0 = pw.Font.ttf(ByteData.sublistView(
File('assets/fonts/NotoSans-Regular.ttf').readAsBytesSync()));
final font1 = pw.Font.ttf(ByteData.sublistView(
File('assets/fonts/NotoSansSC-Regular.ttf').readAsBytesSync()));
final font2 = pw.Font.ttf(ByteData.sublistView(
File('assets/fonts/NotoSansTC-Regular.ttf').readAsBytesSync()));
final font3 = pw.Font.ttf(ByteData.sublistView(
File('assets/fonts/NotoSansJP-Regular.ttf').readAsBytesSync()));
doc.addPage(
pw.MultiPage(
theme: pw.ThemeData.withFont(
base: font3,
fontFallback: [font0, font1, font2],
),
pageFormat: format.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
orientation: pw.PageOrientation.portrait,
crossAxisAlignment: pw.CrossAxisAlignment.start,
header: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const pw.BoxDecoration(
border: pw.Border(
bottom: pw.BorderSide(width: 0.5, color: PdfColors.grey))),
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Text(
'JunoScreen',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey),
),
pw.Text(
'Screening Result',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey),
),
],
),
);
},
footer: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: pw.Text(
'Page ${context.pageNumber} of ${context.pagesCount}',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey),
),
);
},
build: (pw.Context context) => <pw.Widget>[
pw.Header(
level: 2,
title: 'Screeing Status',
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
children: <pw.Widget>[
pw.Text('Screening Status', textScaleFactor: 2),
],
),
),
pw.Paragraph(text: 'Input string: ${result.queryStatus.inputString}'),
pw.Paragraph(
text:
'Preprocessed Name: |${result.queryStatus.terms.map((e) => '${e.string}|').join()}'),
pw.Paragraph(
text:
'Query Score: ${(result.queryStatus.queryScore * 100).floor()}'),
pw.Paragraph(
text:
'Screening Date/ Time: ${result.queryStatus.start.toUtc().toIso8601String()}'),
pw.Paragraph(
text: 'Database Version: ${result.queryStatus.databaseVersion}'),
pw.Paragraph(
text: 'Number of Detected Items: ${result.detectedItems.length}'),
pw.Header(
level: 2,
title: 'Detected Items',
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
children: <pw.Widget>[
pw.Text('Detected Items', textScaleFactor: 2),
],
),
),
pw.TableHelper.fromTextArray(
context: context,
columnWidths: {
0: pw.IntrinsicColumnWidth(),
1: pw.IntrinsicColumnWidth(),
2: pw.IntrinsicColumnWidth(),
3: pw.FlexColumnWidth(),
},
data: [...itemList(result)],
),
// pw.Signature( // needs paid library
// name: 'JunoScreen',
// value: PdfSign(
// privateKey:
// PdfSign.pemPrivateKey(File('key.pem').readAsStringSync()),
// certificates: <Uint8List>[
// PdfSign.pemCertificate(File('cert.pem').readAsStringSync()),
// ],
// ),
// ),
],
),
);
/*
doc.addPage(
pw.MultiPage(
theme: pw.ThemeData.withFont(
base: font1,
bold: font2,
),
pageFormat: format.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
orientation: pw.PageOrientation.portrait,
crossAxisAlignment: pw.CrossAxisAlignment.start,
header: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
decoration: const pw.BoxDecoration(
border: pw.Border(
bottom: pw.BorderSide(width: 0.5, color: PdfColors.grey))),
child: pw.Text(
'Screening Result',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey),
),
);
},
footer: (pw.Context context) {
return pw.Container(
alignment: pw.Alignment.centerRight,
margin: const pw.EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: pw.Text(
'Page ${context.pageNumber} of ${context.pagesCount}',
style: pw.Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey),
),
);
},
build: (pw.Context context) => <pw.Widget>[
pw.Header(
level: 2,
title: 'Details of Detected Items',
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
children: <pw.Widget>[
pw.Text('Details of Detected Items', textScaleFactor: 2),
],
),
),
pw.Column(children: [...itemDetails(result)]),
],
),
);
*/
return await doc.save();
}