123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- 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<void> downloadPDF(
- String fileName,
- ReportDataPipe pipe,
- Future<bool> Function(List<int> 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<String>? 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<String>? images,
- Future<bool> Function(List<int> 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<bool> Function(List<int> data, String fileName)? pdfExporter,
- ) async {
- try {
- ResourcesCache.clearCachedPDF();
- var bytes = await downloadFile(fileUrl);
- _handelDownload(bytes, fileName, pdfExporter);
- } catch (e) {
- debugPrint(e.toString());
- }
- }
- static Future<Uint8List> 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<void> _handelDownload(
- Uint8List bytes,
- String fileName,
- Future<bool> Function(List<int> 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<Uint8List?> Function(String fileName)? cacheGet;
- final Future<bool> Function(String fileName, Uint8List file)? cacheSave;
- ReportDataPipe({
- required this.cacheGet,
- required this.cacheSave,
- });
- }
|