printReportTable method

Future<void> printReportTable(
  1. DataReportTable reportData,
  2. String paperSize, {
  3. bool landscape = false,
  4. List<int>? columnFlexes,
  5. bool isView = false,
  6. bool use_highlighter = false,
  7. bool showSequence = false,
})

Generates and optionally prints or previews a PDF table report.

This method builds a table-based PDF document using the provided reportData. reportData has tableData and tableSummary, and more... The layout can be customized using paperSize, landscape, and columnFlexes. The PDF can be previewed or printed based on the platform and the isView flag.

  • On Web: Always previews the PDF.
  • On Windows: If isView is true, opens a temporary file in the default viewer; otherwise prints directly.
  • On Android/iOS/Linux/macOS: Opens or previews the PDF using the available tools.

Parameters:

  • reportData: Contains the table data, summary, metadata, and other information to render the report.
  • paperSize: Specifies the size of the PDF page (e.g., 'A4').
  • landscape: If true, renders the report in landscape mode. Default is false.
  • columnFlexes: Optional. Defines the flex values (column widths) for the table.
  • isView: On supported platforms, controls whether to view or print directly. Default is false.
  • use_highlighter: If true, applies a highlight style to the table. This will uses last column to highlight rows

Returns a Future that completes when the operation finishes.

Implementation

Future<void> printReportTable(
  DataReportTable reportData,
  String paperSize, {
  bool landscape = false,
  List<int>? columnFlexes, // New optional parameter
  bool isView = false, // New parameter for view/print control
  bool use_highlighter = false,
  bool showSequence = false,
}) async {
  // Default to flex value of 1 for all columns if not provided
  final flexes =
      columnFlexes ?? List.filled(reportData.tableData.first.length, 1);

  final pdf = pw.Document();

  pdf.addPage(
    await TableReportTemplates.buildReportTable(
      paperSize,
      tableData: reportData.tableData,
      tableSummary: reportData.tableSummary,
      language: reportData.language,
      date: reportData.date,
      fromDate: reportData.fromDate,
      toDate: reportData.toDate,
      companyName: reportData.companyName!,
      address: reportData.address!,
      landscape: landscape,
      columnFlexes: flexes, // Pass the flex values
      useHighlighter: use_highlighter,
      showSequence: showSequence,
    ),
  );

  final pdfBytes = await pdf.save();

  if (kIsWeb) {
    // Handle web preview
    await Printing.layoutPdf(
      onLayout: (PdfPageFormat format) async => pdfBytes,
    );
  } else if (UniversalPlatform.isWindows) {
    // Handle Windows with view/print option
    if (isView) {
      // Get the temporary directory
      final tempDir = await getTemporaryDirectory();
      final tempFilePath = '${tempDir.path}/temp_invoice.pdf';
      final tempFile = File(tempFilePath);

      // Write PDF to temporary file
      await tempFile.writeAsBytes(pdfBytes);

      // Open with default PDF viewer
      await OpenFile.open(tempFilePath);
    } else {
      // Direct print
      await Printing.layoutPdf(
        onLayout: (PdfPageFormat format) async => pdfBytes,
        usePrinterSettings: true,
      );
    }
  } else if (UniversalPlatform.isLinux || UniversalPlatform.isMacOS) {
    // Handle other desktop platforms
    await Printing.layoutPdf(
      onLayout: (PdfPageFormat format) async => pdfBytes,
    );
  } else if (UniversalPlatform.isAndroid || UniversalPlatform.isIOS) {
    final tempDir = await getTemporaryDirectory();
    final tempFilePath = '${tempDir.path}/temp_table_report.pdf';
    final tempFile = File(tempFilePath);
    await tempFile.writeAsBytes(pdfBytes);
    await OpenFile.open(tempFilePath);
  }
}