printFile static method

Future<void> printFile({
  1. required String source,
  2. required String fileType,
  3. String? documentName,
  4. BuildContext? context,
})

✅ IMPROVED: Print file with cleanup

Implementation

static Future<void> printFile({
  required String source,
  required String fileType,
  String? documentName,
  BuildContext? context,
}) async {
  FileData? fileData;
  try {
    final ext = _getFileExtension(fileType);
    fileData = await loadFile(source, ext);

    switch (fileType.toLowerCase()) {
      case 'pdf':
        await _printPdf(fileData.bytes, documentName);
        break;
      case 'image':
      case 'img':
      case 'jpg':
      case 'jpeg':
      case 'png':
        await _printImage(fileData.bytes, documentName);
        break;
      case 'text':
      case 'txt':
        await _printText(fileData.bytes, documentName);
        break;
      default:
        throw Exception('Unsupported file type: $fileType');
    }
  } catch (e) {
    if (context != null && context.mounted) {
      _showError(context, 'Print error: $e');
    }
    rethrow;
  } finally {
    // ✅ Cleanup temp file after printing
    await fileData?.cleanup();
  }
}