exportToPdf static method

Future<void> exportToPdf({
  1. required ReportTemplate template,
  2. required List data,
  3. required String filePath,
  4. PdfExportOptions options = const PdfExportOptions(),
})

Exports a template with data to PDF

Implementation

static Future<void> exportToPdf({
  required ReportTemplate template,
  required List<dynamic> data,
  required String filePath,
  PdfExportOptions options = const PdfExportOptions(),
}) async {
  final pdf = pw.Document();

  final pageFormat = _getPageFormat(template);

  for (int i = 0; i < data.length; i++) {
    final item = data[i];

    if (template.reportType == ReportType.label) {
      // Multiple labels per page
      await _addLabelsPage(pdf, template, item, pageFormat, options);
    } else {
      // Single document
      await _addDocumentPage(pdf, template, item, pageFormat, options);
    }
  }

  final file = File(filePath);
  final bytes = await pdf.save();
  await file.writeAsBytes(bytes);
}