savePdfToDeviceAndOpen method
Save PDF to device storage with automatic opening
Implementation
Future<String?> savePdfToDeviceAndOpen(Uint8List pdfBytes, {String? fileName}) async {
try {
final directory = await getApplicationDocumentsDirectory();
final timestamp = DateTime.now().millisecondsSinceEpoch;
final file = File('${directory.path}/${fileName ?? 'lokotro_receipt_$timestamp'}.pdf');
await file.writeAsBytes(pdfBytes);
// Try to open the PDF with default reader
try {
final result = await OpenFilex.open(file.path);
if (result.type == ResultType.done) {
return file.path;
}
} catch (e) {
// If opening fails, still return the path as file was saved
}
return file.path;
} catch (e) {
return null;
}
}