generateEntriesReport function

Future<void> generateEntriesReport({
  1. required List<EntryModel> items,
  2. required String branchName,
  3. required String branchAddress,
  4. required BuildContext context,
  5. required String bankName,
  6. required DateTime startDate,
  7. required DateTime endDate,
})

Implementation

Future<void> generateEntriesReport({
  required List<EntryModel> items,
  required String branchName,
  required String branchAddress,
  required BuildContext context,
  required String bankName,
  required DateTime startDate,
  required DateTime endDate,
}) async {
  try {
    final doc = pw.Document();
    // Add PDF page
    doc.addPage(
      pw.MultiPage(
        margin: const pw.EdgeInsets.symmetric(horizontal: 36, vertical: 46),
        footer: (pw.Context context) {
          return pw.Container(
            alignment: pw.Alignment.centerRight,
            margin: const pw.EdgeInsets.only(top: 10),
            child: pw.Text(
              'Page ${context.pageNumber.abs()} of ${context.pagesCount}',
              style:
                  pw.TextStyle(fontSize: 8, fontWeight: pw.FontWeight.normal),
            ),
          );
        },
        build: (pw.Context ctx) {
          return <pw.Widget>[
            // Header Section
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.center,
              mainAxisAlignment: pw.MainAxisAlignment.center,
              children: [
                pw.SizedBox(width: 10),
                pw.Column(
                  crossAxisAlignment: pw.CrossAxisAlignment.center,
                  children: [
                    pw.Text(bankName,
                        style: pw.TextStyle(
                            fontSize: 10, fontWeight: pw.FontWeight.bold)),
                    pw.Text(branchName, style: const pw.TextStyle(fontSize: 8)),
                    pw.SizedBox(height: 2),
                    pw.Text(branchAddress,
                        style: const pw.TextStyle(fontSize: 8)),
                    pw.SizedBox(height: 2),
                    pw.Text('Transaction Entries',
                        style: pw.TextStyle(
                            fontSize: 10, fontWeight: pw.FontWeight.bold)),
                    pw.Text(
                      'From: ${DateFormat('MM-dd-yyyy').format(startDate.toLocal())} '
                      'To: ${DateFormat('MM-dd-yyyy').format(endDate.toLocal())}',
                      style: pw.TextStyle(
                          fontSize: 8, fontWeight: pw.FontWeight.bold),
                    ),
                  ],
                ),
              ],
            ),
            pw.SizedBox(height: 10),

            pw.Table(
              children: [
                // Headers
                pw.TableRow(
                  decoration: pw.BoxDecoration(
                      border: pw.Border.symmetric(
                    horizontal: pw.BorderSide(
                      color: PdfColors.grey,
                      width: 0,
                    ),
                  )),
                  children: [
                    _buildHeaderCell('Banking Date'),
                    _buildHeaderCell('Account No.'),
                    _buildHeaderCell('Currency'),
                    _buildHeaderCell('Debit Amount'),
                    _buildHeaderCell('Credit Amount'),
                    _buildHeaderCell('Description'),
                    _buildHeaderCell('Reference No.'),
                  ],
                ),
                // Data Rows
                for (var item in items)
                  pw.TableRow(
                    children: [
                      _buildDataCell(DateFormat('MM/dd/yyyy')
                          .format(item.branchDate ?? DateTime.now())),
                      _buildDataCell(item.accountNumber ?? ''),
                      _buildDataCell(item.currency ?? ''),
                      _buildDataCell(item.debitAmount == null ||
                              item.debitAmount?.abs() == 0.00
                          ? "0.00"
                          : Constants.formatAmount(item.debitAmount!.abs())),
                      _buildDataCell(item.creditAmount == null ||
                              item.creditAmount?.abs() == 0.00
                          ? "0.00"
                          : Constants.formatAmount(item.creditAmount?.abs())),
                      _buildDataCell(item.transactionType ?? ''),
                      _buildDataCell(item.transactionNumber ?? ''),
                    ],
                  ),
              ],
            ),
          ];
        },
      ),
    );

    // Print or Save PDF
    // Save PDF as bytes
    final pdfBytes = await doc.save();

    // Create a blob and open in a new HTML page (for Flutter web)
    // ignore: undefined_prefixed_name
    final blob = html.Blob([pdfBytes], 'application/pdf');
    final url = html.Url.createObjectUrlFromBlob(blob);
    // ignore: undefined_prefixed_name
    html.window.open(url, '_blank');
    html.Url.revokeObjectUrl(url);
  } catch (err) {
    log("Error: $err");
    return;
  }
}