printClassicTable method

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

Implementation

Future<void> printClassicTable(
  DataClassicTable reportData,
  String paperSize, {
  bool landscape = false,
  List<int>? columnFlexes, // New optional parameter
  bool isView = false, // New parameter for view/print control
  bool use_RowFooter = 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.buildClassicTable(
      paperSize,
      tableData: reportData.tableData,
      tableSummary: reportData.tableSummary,
      headerInformation: reportData.headerInformation,
      language: reportData.language,
      date: reportData.date,
      companyName: reportData.companyName!,
      address: reportData.address!,
      landscape: landscape,
      columnFlexes: flexes, // Pass the flex values
      use_RowFooter: use_RowFooter,
      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);
  }
}