Showing
4 changed files
with
44 additions
and
10 deletions
| @@ -24,12 +24,17 @@ class PdfPrintPageRenderer: UIPrintPageRenderer { | @@ -24,12 +24,17 @@ class PdfPrintPageRenderer: UIPrintPageRenderer { | ||
| 24 | private var channel: FlutterMethodChannel? | 24 | private var channel: FlutterMethodChannel? |
| 25 | private var pdfDocument: CGPDFDocument? | 25 | private var pdfDocument: CGPDFDocument? |
| 26 | private var lock: NSLock? | 26 | private var lock: NSLock? |
| 27 | + private var mustLayout: Bool = true | ||
| 27 | 28 | ||
| 28 | - init(_ channel: FlutterMethodChannel?) { | 29 | + init(_ channel: FlutterMethodChannel?, data: Data?) { |
| 29 | super.init() | 30 | super.init() |
| 30 | self.channel = channel | 31 | self.channel = channel |
| 31 | - lock = NSLock() | ||
| 32 | pdfDocument = nil | 32 | pdfDocument = nil |
| 33 | + if data != nil { | ||
| 34 | + setDocument(data) | ||
| 35 | + mustLayout = false | ||
| 36 | + } | ||
| 37 | + lock = NSLock() | ||
| 33 | } | 38 | } |
| 34 | 39 | ||
| 35 | override func drawPage(at pageIndex: Int, in _: CGRect) { | 40 | override func drawPage(at pageIndex: Int, in _: CGRect) { |
| @@ -72,10 +77,12 @@ class PdfPrintPageRenderer: UIPrintPageRenderer { | @@ -72,10 +77,12 @@ class PdfPrintPageRenderer: UIPrintPageRenderer { | ||
| 72 | "marginBottom": marginBottom, | 77 | "marginBottom": marginBottom, |
| 73 | ] | 78 | ] |
| 74 | 79 | ||
| 80 | + if mustLayout { | ||
| 75 | lock?.lock() | 81 | lock?.lock() |
| 76 | channel?.invokeMethod("onLayout", arguments: arg) | 82 | channel?.invokeMethod("onLayout", arguments: arg) |
| 77 | lock?.lock() | 83 | lock?.lock() |
| 78 | lock?.unlock() | 84 | lock?.unlock() |
| 85 | + } | ||
| 79 | 86 | ||
| 80 | let pages = pdfDocument?.numberOfPages ?? 0 | 87 | let pages = pdfDocument?.numberOfPages ?? 0 |
| 81 | 88 |
| @@ -39,7 +39,8 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | @@ -39,7 +39,8 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | ||
| 39 | let args = call.arguments! as! [String: Any] | 39 | let args = call.arguments! as! [String: Any] |
| 40 | if call.method == "printPdf" { | 40 | if call.method == "printPdf" { |
| 41 | let name = args["name"] as? String ?? "" | 41 | let name = args["name"] as? String ?? "" |
| 42 | - printPdf(name) | 42 | + let object = args["doc"] as? FlutterStandardTypedData |
| 43 | + printPdf(name, data: object?.data) | ||
| 43 | result(NSNumber(value: 1)) | 44 | result(NSNumber(value: 1)) |
| 44 | } else if call.method == "directPrintPdf" { | 45 | } else if call.method == "directPrintPdf" { |
| 45 | let name = args["name"] as? String ?? "" | 46 | let name = args["name"] as? String ?? "" |
| @@ -49,7 +50,7 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | @@ -49,7 +50,7 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | ||
| 49 | result(NSNumber(value: 1)) | 50 | result(NSNumber(value: 1)) |
| 50 | } else if call.method == "writePdf" { | 51 | } else if call.method == "writePdf" { |
| 51 | if let object = args["doc"] as? FlutterStandardTypedData { | 52 | if let object = args["doc"] as? FlutterStandardTypedData { |
| 52 | - writePdf(object) | 53 | + writePdf(object.data) |
| 53 | } | 54 | } |
| 54 | result(NSNumber(value: 1)) | 55 | result(NSNumber(value: 1)) |
| 55 | } else if call.method == "cancelJob" { | 56 | } else if call.method == "cancelJob" { |
| @@ -97,6 +98,11 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | @@ -97,6 +98,11 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | ||
| 97 | width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0), | 98 | width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0), |
| 98 | height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0) | 99 | height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0) |
| 99 | )) | 100 | )) |
| 101 | + } else if call.method == "printingInfo" { | ||
| 102 | + let data: NSDictionary = [ | ||
| 103 | + "iosVersion": UIDevice.current.systemVersion, | ||
| 104 | + ] | ||
| 105 | + result(data) | ||
| 100 | } else { | 106 | } else { |
| 101 | result(FlutterMethodNotImplemented) | 107 | result(FlutterMethodNotImplemented) |
| 102 | } | 108 | } |
| @@ -150,7 +156,7 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | @@ -150,7 +156,7 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | ||
| 150 | controller.print(to: printer, completionHandler: completionHandler) | 156 | controller.print(to: printer, completionHandler: completionHandler) |
| 151 | } | 157 | } |
| 152 | 158 | ||
| 153 | - func printPdf(_ name: String) { | 159 | + func printPdf(_ name: String, data: Data?) { |
| 154 | let printing = UIPrintInteractionController.isPrintingAvailable | 160 | let printing = UIPrintInteractionController.isPrintingAvailable |
| 155 | if !printing { | 161 | if !printing { |
| 156 | let data: NSDictionary = [ | 162 | let data: NSDictionary = [ |
| @@ -168,13 +174,13 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | @@ -168,13 +174,13 @@ public class SwiftPrintingPlugin: NSObject, FlutterPlugin, UIPrintInteractionCon | ||
| 168 | printInfo.jobName = name | 174 | printInfo.jobName = name |
| 169 | printInfo.outputType = .general | 175 | printInfo.outputType = .general |
| 170 | controller.printInfo = printInfo | 176 | controller.printInfo = printInfo |
| 171 | - renderer = PdfPrintPageRenderer(channel) | 177 | + renderer = PdfPrintPageRenderer(channel, data: data) |
| 172 | controller.printPageRenderer = renderer | 178 | controller.printPageRenderer = renderer |
| 173 | controller.present(animated: true, completionHandler: completionHandler) | 179 | controller.present(animated: true, completionHandler: completionHandler) |
| 174 | } | 180 | } |
| 175 | 181 | ||
| 176 | - func writePdf(_ data: FlutterStandardTypedData) { | ||
| 177 | - renderer?.setDocument(data.data) | 182 | + func writePdf(_ data: Data) { |
| 183 | + renderer?.setDocument(data) | ||
| 178 | } | 184 | } |
| 179 | 185 | ||
| 180 | func sharePdf(_ data: FlutterStandardTypedData, withSourceRect rect: CGRect, andName name: String?) { | 186 | func sharePdf(_ data: FlutterStandardTypedData, withSourceRect rect: CGRect, andName name: String?) { |
| @@ -96,15 +96,35 @@ mixin Printing { | @@ -96,15 +96,35 @@ mixin Printing { | ||
| 96 | static Future<bool> layoutPdf({ | 96 | static Future<bool> layoutPdf({ |
| 97 | @required LayoutCallback onLayout, | 97 | @required LayoutCallback onLayout, |
| 98 | String name = 'Document', | 98 | String name = 'Document', |
| 99 | + PdfPageFormat format = PdfPageFormat.standard, | ||
| 99 | }) async { | 100 | }) async { |
| 100 | _onCompleted = Completer<bool>(); | 101 | _onCompleted = Completer<bool>(); |
| 101 | _onLayout = onLayout; | 102 | _onLayout = onLayout; |
| 102 | _channel.setMethodCallHandler(_handleMethod); | 103 | _channel.setMethodCallHandler(_handleMethod); |
| 103 | final Map<String, dynamic> params = <String, dynamic>{'name': name}; | 104 | final Map<String, dynamic> params = <String, dynamic>{'name': name}; |
| 105 | + try { | ||
| 106 | + final Map<dynamic, dynamic> info = await printingInfo(); | ||
| 107 | + if (int.parse(info['iosVersion'].toString().split('.').first) >= 13) { | ||
| 108 | + final List<int> bytes = await onLayout(format); | ||
| 109 | + if (bytes == null) { | ||
| 110 | + return false; | ||
| 111 | + } | ||
| 112 | + params['doc'] = Uint8List.fromList(bytes); | ||
| 113 | + } | ||
| 114 | + } catch (e) { | ||
| 115 | + e.toString(); | ||
| 116 | + } | ||
| 104 | await _channel.invokeMethod<int>('printPdf', params); | 117 | await _channel.invokeMethod<int>('printPdf', params); |
| 105 | return _onCompleted.future; | 118 | return _onCompleted.future; |
| 106 | } | 119 | } |
| 107 | 120 | ||
| 121 | + static Future<Map<dynamic, dynamic>> printingInfo() async { | ||
| 122 | + return await _channel.invokeMethod<Map<dynamic, dynamic>>( | ||
| 123 | + 'printingInfo', | ||
| 124 | + <String, dynamic>{}, | ||
| 125 | + ); | ||
| 126 | + } | ||
| 127 | + | ||
| 108 | /// Opens the native printer picker interface, and returns the URL of the selected printer. | 128 | /// Opens the native printer picker interface, and returns the URL of the selected printer. |
| 109 | static Future<Printer> pickPrinter({Rect bounds}) async { | 129 | static Future<Printer> pickPrinter({Rect bounds}) async { |
| 110 | _channel.setMethodCallHandler(_handleMethod); | 130 | _channel.setMethodCallHandler(_handleMethod); |
| @@ -117,7 +137,6 @@ mixin Printing { | @@ -117,7 +137,6 @@ mixin Printing { | ||
| 117 | }; | 137 | }; |
| 118 | final Map<dynamic, dynamic> printer = await _channel | 138 | final Map<dynamic, dynamic> printer = await _channel |
| 119 | .invokeMethod<Map<dynamic, dynamic>>('pickPrinter', params); | 139 | .invokeMethod<Map<dynamic, dynamic>>('pickPrinter', params); |
| 120 | - print(printer); | ||
| 121 | if (printer == null) { | 140 | if (printer == null) { |
| 122 | return null; | 141 | return null; |
| 123 | } | 142 | } |
| @@ -138,10 +157,11 @@ mixin Printing { | @@ -138,10 +157,11 @@ mixin Printing { | ||
| 138 | @required Printer printer, | 157 | @required Printer printer, |
| 139 | @required LayoutCallback onLayout, | 158 | @required LayoutCallback onLayout, |
| 140 | String name = 'Document', | 159 | String name = 'Document', |
| 160 | + PdfPageFormat format = PdfPageFormat.standard, | ||
| 141 | }) async { | 161 | }) async { |
| 142 | _onCompleted = Completer<bool>(); | 162 | _onCompleted = Completer<bool>(); |
| 143 | _channel.setMethodCallHandler(_handleMethod); | 163 | _channel.setMethodCallHandler(_handleMethod); |
| 144 | - final List<int> bytes = await onLayout(PdfPageFormat.standard); | 164 | + final List<int> bytes = await onLayout(format); |
| 145 | if (bytes == null) { | 165 | if (bytes == null) { |
| 146 | return false; | 166 | return false; |
| 147 | } | 167 | } |
-
Please register or login to post a comment