pdfhelper.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import 'dart:convert';
  2. import 'package:fis_common/env/env.dart';
  3. import 'package:fis_common/js_plateform/js_platform.dart';
  4. import 'package:fis_lib_print/printing.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flyinsonolite/consultation/reports/pdf/reportrender.dart';
  7. import 'package:flyinsonolite/consultation/reports/pdf/resourcescache.dart';
  8. import 'package:pdf/pdf.dart';
  9. import 'package:http/http.dart' as http;
  10. /// PDF 各项功能【打印】【导出】
  11. class PDFHelper {
  12. PDFHelper();
  13. /// 打印文档格式的 PDF
  14. static void printPDF(String fileName, ReportDataPipe pipe) async {
  15. try {
  16. _preProcessData(fileName, pipe);
  17. var bytes = await generateReport(PdfPageFormat.a4, null);
  18. _handelPrint(bytes);
  19. } catch (e) {
  20. debugPrint(e.toString());
  21. }
  22. }
  23. /// 下载(导出)文档格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】
  24. static Future<void> downloadPDF(
  25. String fileName,
  26. ReportDataPipe pipe,
  27. Future<bool> Function(List<int> data, String fileName)? pdfExporter,
  28. ) async {
  29. try {
  30. _preProcessData(fileName, pipe);
  31. var bytes = await generateReport(PdfPageFormat.a4, null);
  32. await _handelDownload(bytes, fileName, pdfExporter);
  33. } catch (e) {
  34. debugPrint(e.toString());
  35. }
  36. }
  37. /// 打印图片格式的 PDF
  38. static void printPDFJpges(String fileName, List<String>? images) async {
  39. try {
  40. ResourcesCache.clearCachedPDF();
  41. var bytes = await generateByGivenPDFJpges(PdfPageFormat.a4, images);
  42. _handelPrint(bytes);
  43. } catch (e) {
  44. debugPrint(e.toString());
  45. }
  46. }
  47. /// 打印文件格式的 PDF
  48. static void printPDFFile(String fileName, String fileUrl) async {
  49. try {
  50. ResourcesCache.clearCachedPDF();
  51. var bytes = await downloadFile(fileUrl);
  52. _handelPrint(bytes);
  53. } catch (e) {
  54. debugPrint(e.toString());
  55. }
  56. }
  57. /// 下载(导出)图片格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】
  58. static void downloadPDFJpges(
  59. String fileName,
  60. List<String>? images,
  61. Future<bool> Function(List<int> data, String fileName)? pdfExporter,
  62. ) async {
  63. try {
  64. ResourcesCache.clearCachedPDF();
  65. var bytes = await generateByGivenPDFJpges(PdfPageFormat.a4, images);
  66. _handelDownload(bytes, fileName, pdfExporter);
  67. } catch (e) {
  68. debugPrint(e.toString());
  69. }
  70. }
  71. /// 下载(导出)文件格式 PDF【在壳子环境下会调用壳子提供的文件导出能力】
  72. static void downloadPDFFile(
  73. String fileName,
  74. String fileUrl,
  75. Future<bool> Function(List<int> data, String fileName)? pdfExporter,
  76. ) async {
  77. try {
  78. ResourcesCache.clearCachedPDF();
  79. var bytes = await downloadFile(fileUrl);
  80. _handelDownload(bytes, fileName, pdfExporter);
  81. } catch (e) {
  82. debugPrint(e.toString());
  83. }
  84. }
  85. static Future<Uint8List> downloadFile(String fileUrl) async {
  86. var response = await http.get(Uri.parse(fileUrl));
  87. if (response.statusCode == 200) {
  88. return response.bodyBytes;
  89. } else {
  90. throw Exception('Failed to download file');
  91. }
  92. }
  93. /// 处理打印的细节
  94. static void _handelPrint(Uint8List bytes) {
  95. var base64 = base64Encode(bytes);
  96. JSPlateForm.printPdf(base64);
  97. }
  98. /// 处理下载的细节
  99. static Future<void> _handelDownload(
  100. Uint8List bytes,
  101. String fileName,
  102. Future<bool> Function(List<int> data, String fileName)? pdfExporter,
  103. ) async {
  104. if (_useFileExporertUI()) {
  105. await pdfExporter!.call(bytes, fileName);
  106. return;
  107. }
  108. await Printing.sharePdf(
  109. bytes: bytes,
  110. filename: fileName,
  111. );
  112. }
  113. /// 下载(导出)时使用系统UI下载文件
  114. static bool _useFileExporertUI() {
  115. final platform = FPlatform.current;
  116. return platform == FPlatformEnum.webOnWin ||
  117. platform == FPlatformEnum.webOnMac;
  118. }
  119. /// 数据获取预处理
  120. static _preProcessData(String fileName, ReportDataPipe pipe) {
  121. ResourcesCache.clearCachedPDF();
  122. ResourcesCache.currentPDFFileKey = fileName;
  123. ResourcesCache.cacheGet = pipe.cacheGet;
  124. ResourcesCache.cacheSave = pipe.cacheSave;
  125. }
  126. }
  127. /// 数据读写通道
  128. class ReportDataPipe {
  129. final Future<Uint8List?> Function(String fileName)? cacheGet;
  130. final Future<bool> Function(String fileName, Uint8List file)? cacheSave;
  131. ReportDataPipe({
  132. required this.cacheGet,
  133. required this.cacheSave,
  134. });
  135. }