Skip to content

Commit

Permalink
Use WKWebView on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr committed Nov 28, 2024
1 parent f4f8a5e commit 9f213a8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
4 changes: 4 additions & 0 deletions printing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 5.14.0

- Replace WebView with WKWebView on macOS

## 5.13.4

- Improve Web Browser detection logic
Expand Down
31 changes: 17 additions & 14 deletions printing/macos/Classes/PrintJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
// }
}

@available(macOS 11.0, *)
public func convertHtml(_ data: String, withPageSize size: CGRect, andMargin margin: CGRect, andBaseUrl baseUrl: URL?) {
let tempFile = NSTemporaryDirectory() + NSUUID().uuidString
let directoryURL = URL(fileURLWithPath: tempFile)
Expand All @@ -230,23 +231,20 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
printInfo.rightMargin = size.width - margin.maxX
printInfo.bottomMargin = size.height - margin.maxY

let webView = WebView()
webView.mainFrame.loadHTMLString(data, baseURL: baseUrl)
let webView = WKWebView(frame: CGRect.zero)
webView.loadHTMLString(data, baseURL: baseUrl)
let when = DispatchTime.now() + 1

DispatchQueue.main.asyncAfter(deadline: when) {
let printOperation = NSPrintOperation(view: webView.mainFrame.frameView.documentView, printInfo: printInfo)
printOperation.showsPrintPanel = false
printOperation.showsProgressPanel = false
printOperation.run()

do {
let data = try Data(contentsOf: directoryURL)
self.printing.onHtmlRendered(printJob: self, pdfData: data)
let fileManager = FileManager.default
try fileManager.removeItem(atPath: tempFile)
} catch {
self.printing.onHtmlError(printJob: self, error: "Unable to load the pdf file from \(tempFile)")
webView.createPDF { result in
switch result {
case .success(let data):
self.printing.onHtmlRendered(printJob: self, pdfData: data)
let fileManager = FileManager.default
try? fileManager.removeItem(atPath: tempFile)
case .failure(let error):
self.printing.onHtmlError(printJob: self, error: "Unable to create PDF: \(error.localizedDescription)")
}
}
}
}
Expand Down Expand Up @@ -305,10 +303,15 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate {
}

public static func printingInfo() -> NSDictionary {
var html = false
if #available(macOS 11.0, *) {
html = true
}
let data: NSDictionary = [
"directPrint": true,
"dynamicLayout": true,
"canPrint": true,
"canConvertHtml": html,
"canShare": true,
"canRaster": true,
"canListPrinters": true,
Expand Down
38 changes: 21 additions & 17 deletions printing/macos/Classes/PrintingPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,27 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
let marginBottom = CGFloat((args["marginBottom"] as? NSNumber)?.floatValue ?? 0.0)
let printJob = PrintJob(printing: self, index: args["job"] as! Int)

printJob.convertHtml(
args["html"] as! String,
withPageSize: CGRect(
x: 0.0,
y: 0.0,
width: width,
height: height
),
andMargin: CGRect(
x: marginLeft,
y: marginTop,
width: width - marginRight - marginLeft,
height: height - marginBottom - marginTop
),
andBaseUrl: args["baseUrl"] as? String == nil ? nil : URL(string: args["baseUrl"] as! String)
)
result(NSNumber(value: 1))
if #available(macOS 11.0, *) {
printJob.convertHtml(
args["html"] as! String,
withPageSize: CGRect(
x: 0.0,
y: 0.0,
width: width,
height: height
),
andMargin: CGRect(
x: marginLeft,
y: marginTop,
width: width - marginRight - marginLeft,
height: height - marginBottom - marginTop
),
andBaseUrl: args["baseUrl"] as? String == nil ? nil : URL(string: args["baseUrl"] as! String)
)
result(NSNumber(value: 1))
} else {
result(NSNumber(value: 0))
}
} else if call.method == "printingInfo" {
result(PrintJob.printingInfo())
} else if call.method == "rasterPdf" {
Expand Down
2 changes: 1 addition & 1 deletion printing/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ topics:
- print
- printing
- report
version: 5.13.4
version: 5.14.0

environment:
sdk: ">=3.3.0 <4.0.0"
Expand Down

0 comments on commit 9f213a8

Please sign in to comment.