printPaymentInvoice method

Future<void> printPaymentInvoice(
  1. DataPaymentInvoice reportData,
  2. String paperSize,
  3. bool isDuplicate
)

Implementation

Future<void> printPaymentInvoice(
  DataPaymentInvoice reportData,
  String paperSize,
  bool isDuplicate,
) async {
  final pdf = pw.Document();
  pdf.addPage(
    await PaymentInvoiceTemplate.buildInvoice(
      paperSize,
      companyName: reportData.companyName!,
      invoiceNumber: reportData.invoiceNumber,
      phoneNumber: reportData.phoneNumber!,
      invoiceTitle: reportData.invoiceTitle!,
      date: reportData.date,
      recipientName: reportData.recipientName,
      amount: reportData.amount,
      amountInWords: reportData.amountInWords!,
      note: reportData.note,
      account: reportData.totalAccount,
      paid: reportData.totalPaid,
      authorizedSignature: reportData.authorizedSignature,
      address: reportData.address!,
      language: reportData.language,
      duplicate: isDuplicate,
    ),
  );
  final pdfBytes = await pdf.save();
  if (kIsWeb ||
      UniversalPlatform.isWindows ||
      UniversalPlatform.isLinux ||
      UniversalPlatform.isMacOS) {
    // Handle print preview for web and desktop
    await Printing.layoutPdf(
      onLayout: (PdfPageFormat format) async => pdfBytes,
    );
  } else if (UniversalPlatform.isAndroid || UniversalPlatform.isIOS) {
    // Handle mobile print preview or opening PDF in viewer
    final tempDir = await getTemporaryDirectory();
    final tempFilePath = '${tempDir.path}/temp_invoice.pdf';
    final tempFile = File(tempFilePath);

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

    // Open the file using a PDF viewer or print preview on mobile
    await OpenFile.open(tempFilePath);
  }
}