Showing
4 changed files
with
43 additions
and
32 deletions
@@ -215,6 +215,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -215,6 +215,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
215 | // } | 215 | // } |
216 | } | 216 | } |
217 | 217 | ||
218 | + @available(macOS 11.0, *) | ||
218 | public func convertHtml(_ data: String, withPageSize size: CGRect, andMargin margin: CGRect, andBaseUrl baseUrl: URL?) { | 219 | public func convertHtml(_ data: String, withPageSize size: CGRect, andMargin margin: CGRect, andBaseUrl baseUrl: URL?) { |
219 | let tempFile = NSTemporaryDirectory() + NSUUID().uuidString | 220 | let tempFile = NSTemporaryDirectory() + NSUUID().uuidString |
220 | let directoryURL = URL(fileURLWithPath: tempFile) | 221 | let directoryURL = URL(fileURLWithPath: tempFile) |
@@ -230,23 +231,20 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -230,23 +231,20 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
230 | printInfo.rightMargin = size.width - margin.maxX | 231 | printInfo.rightMargin = size.width - margin.maxX |
231 | printInfo.bottomMargin = size.height - margin.maxY | 232 | printInfo.bottomMargin = size.height - margin.maxY |
232 | 233 | ||
233 | - let webView = WebView() | ||
234 | - webView.mainFrame.loadHTMLString(data, baseURL: baseUrl) | 234 | + let webView = WKWebView(frame: CGRect.zero) |
235 | + webView.loadHTMLString(data, baseURL: baseUrl) | ||
235 | let when = DispatchTime.now() + 1 | 236 | let when = DispatchTime.now() + 1 |
236 | 237 | ||
237 | DispatchQueue.main.asyncAfter(deadline: when) { | 238 | DispatchQueue.main.asyncAfter(deadline: when) { |
238 | - let printOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo) | ||
239 | - printOperation.showsPrintPanel = false | ||
240 | - printOperation.showsProgressPanel = false | ||
241 | - printOperation.run() | ||
242 | - | ||
243 | - do { | ||
244 | - let data = try Data(contentsOf: directoryURL) | ||
245 | - self.printing.onHtmlRendered(printJob: self, pdfData: data) | ||
246 | - let fileManager = FileManager.default | ||
247 | - try fileManager.removeItem(atPath: tempFile) | ||
248 | - } catch { | ||
249 | - self.printing.onHtmlError(printJob: self, error: "Unable to load the pdf file from \(tempFile)") | 239 | + webView.createPDF { result in |
240 | + switch result { | ||
241 | + case .success(let data): | ||
242 | + self.printing.onHtmlRendered(printJob: self, pdfData: data) | ||
243 | + let fileManager = FileManager.default | ||
244 | + try? fileManager.removeItem(atPath: tempFile) | ||
245 | + case .failure(let error): | ||
246 | + self.printing.onHtmlError(printJob: self, error: "Unable to create PDF: \(error.localizedDescription)") | ||
247 | + } | ||
250 | } | 248 | } |
251 | } | 249 | } |
252 | } | 250 | } |
@@ -305,10 +303,15 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -305,10 +303,15 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
305 | } | 303 | } |
306 | 304 | ||
307 | public static func printingInfo() -> NSDictionary { | 305 | public static func printingInfo() -> NSDictionary { |
306 | + var html = false | ||
307 | + if #available(macOS 11.0, *) { | ||
308 | + html = true | ||
309 | + } | ||
308 | let data: NSDictionary = [ | 310 | let data: NSDictionary = [ |
309 | "directPrint": true, | 311 | "directPrint": true, |
310 | "dynamicLayout": true, | 312 | "dynamicLayout": true, |
311 | "canPrint": true, | 313 | "canPrint": true, |
314 | + "canConvertHtml": html, | ||
312 | "canShare": true, | 315 | "canShare": true, |
313 | "canRaster": true, | 316 | "canRaster": true, |
314 | "canListPrinters": true, | 317 | "canListPrinters": true, |
@@ -115,23 +115,27 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -115,23 +115,27 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
115 | let marginBottom = CGFloat((args["marginBottom"] as? NSNumber)?.floatValue ?? 0.0) | 115 | let marginBottom = CGFloat((args["marginBottom"] as? NSNumber)?.floatValue ?? 0.0) |
116 | let printJob = PrintJob(printing: self, index: args["job"] as! Int) | 116 | let printJob = PrintJob(printing: self, index: args["job"] as! Int) |
117 | 117 | ||
118 | - printJob.convertHtml( | ||
119 | - args["html"] as! String, | ||
120 | - withPageSize: CGRect( | ||
121 | - x: 0.0, | ||
122 | - y: 0.0, | ||
123 | - width: width, | ||
124 | - height: height | ||
125 | - ), | ||
126 | - andMargin: CGRect( | ||
127 | - x: marginLeft, | ||
128 | - y: marginTop, | ||
129 | - width: width - marginRight - marginLeft, | ||
130 | - height: height - marginBottom - marginTop | ||
131 | - ), | ||
132 | - andBaseUrl: args["baseUrl"] as? String == nil ? nil : URL(string: args["baseUrl"] as! String) | ||
133 | - ) | ||
134 | - result(NSNumber(value: 1)) | 118 | + if #available(macOS 11.0, *) { |
119 | + printJob.convertHtml( | ||
120 | + args["html"] as! String, | ||
121 | + withPageSize: CGRect( | ||
122 | + x: 0.0, | ||
123 | + y: 0.0, | ||
124 | + width: width, | ||
125 | + height: height | ||
126 | + ), | ||
127 | + andMargin: CGRect( | ||
128 | + x: marginLeft, | ||
129 | + y: marginTop, | ||
130 | + width: width - marginRight - marginLeft, | ||
131 | + height: height - marginBottom - marginTop | ||
132 | + ), | ||
133 | + andBaseUrl: args["baseUrl"] as? String == nil ? nil : URL(string: args["baseUrl"] as! String) | ||
134 | + ) | ||
135 | + result(NSNumber(value: 1)) | ||
136 | + } else { | ||
137 | + result(NSNumber(value: 0)) | ||
138 | + } | ||
135 | } else if call.method == "printingInfo" { | 139 | } else if call.method == "printingInfo" { |
136 | result(PrintJob.printingInfo()) | 140 | result(PrintJob.printingInfo()) |
137 | } else if call.method == "rasterPdf" { | 141 | } else if call.method == "rasterPdf" { |
-
Please register or login to post a comment