import 'dart:convert'; import 'package:fis_common/env/env.dart'; import 'package:fis_common/js_plateform/js_platform.dart'; import 'package:fis_lib_print/printing.dart'; import 'package:flutter/foundation.dart'; import 'package:flyinsonolite/consultation/reports/pdf/reportrender.dart'; import 'package:flyinsonolite/consultation/reports/pdf/resourcescache.dart'; import 'package:pdf/pdf.dart'; import 'package:http/http.dart' as http; /// PDF 各项功能【打印】【导出】 class PDFHelper { PDFHelper(); /// 打印文档格式的 PDF static void printPDF(String fileName, ReportDataPipe pipe) async { try { _preProcessData(fileName, pipe); var bytes = await generateReport(PdfPageFormat.a4, null); _handelPrint(bytes); } catch (e) { debugPrint(e.toString()); } } /// 下载(导出)文档格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】 static Future downloadPDF( String fileName, ReportDataPipe pipe, Future Function(List data, String fileName)? pdfExporter, ) async { try { _preProcessData(fileName, pipe); var bytes = await generateReport(PdfPageFormat.a4, null); await _handelDownload(bytes, fileName, pdfExporter); } catch (e) { debugPrint(e.toString()); } } /// 打印图片格式的 PDF static void printPDFJpges(String fileName, List? images) async { try { ResourcesCache.clearCachedPDF(); var bytes = await generateByGivenPDFJpges(PdfPageFormat.a4, images); _handelPrint(bytes); } catch (e) { debugPrint(e.toString()); } } /// 打印文件格式的 PDF static void printPDFFile(String fileName, String fileUrl) async { try { ResourcesCache.clearCachedPDF(); var bytes = await downloadFile(fileUrl); _handelPrint(bytes); } catch (e) { debugPrint(e.toString()); } } /// 下载(导出)图片格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】 static void downloadPDFJpges( String fileName, List? images, Future Function(List data, String fileName)? pdfExporter, ) async { try { ResourcesCache.clearCachedPDF(); var bytes = await generateByGivenPDFJpges(PdfPageFormat.a4, images); _handelDownload(bytes, fileName, pdfExporter); } catch (e) { debugPrint(e.toString()); } } /// 下载(导出)文件格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】 static void downloadPDFFile( String fileName, String fileUrl, Future Function(List data, String fileName)? pdfExporter, ) async { try { ResourcesCache.clearCachedPDF(); var bytes = await downloadFile(fileUrl); _handelDownload(bytes, fileName, pdfExporter); } catch (e) { debugPrint(e.toString()); } } static Future downloadFile(String fileUrl) async { var response = await http.get(Uri.parse(fileUrl)); if (response.statusCode == 200) { return response.bodyBytes; } else { throw Exception('Failed to download file'); } } /// 处理打印的细节 static void _handelPrint(Uint8List bytes) { var base64 = base64Encode(bytes); JSPlateForm.printPdf(base64); } /// 处理下载的细节 static Future _handelDownload( Uint8List bytes, String fileName, Future Function(List data, String fileName)? pdfExporter, ) async { if (_useFileExporertUI()) { await pdfExporter!.call(bytes, fileName); return; } await Printing.sharePdf( bytes: bytes, filename: fileName, ); } /// 下载(导出)时使用系统UI下载文件 static bool _useFileExporertUI() { final platform = FPlatform.current; return platform == FPlatformEnum.webOnWin || platform == FPlatformEnum.webOnMac; } /// 数据获取预处理 static _preProcessData(String fileName, ReportDataPipe pipe) { ResourcesCache.clearCachedPDF(); ResourcesCache.currentPDFFileKey = fileName; ResourcesCache.cacheGet = pipe.cacheGet; ResourcesCache.cacheSave = pipe.cacheSave; } } /// 数据读写通道 class ReportDataPipe { final Future Function(String fileName)? cacheGet; final Future Function(String fileName, Uint8List file)? cacheSave; ReportDataPipe({ required this.cacheGet, required this.cacheSave, }); }