The code bellow works on iPad but not on macOS using "designed for iPad" mode. I get this error.
CGContextClipToRect: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
How can I modify the code to runs correctly on macOS?
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Button("print"){
prt()
}
}
.padding()
}
func prt(){
let htmlString = "<h1>Hello World</h1>"
let formatter: UIMarkupTextPrintFormatter = UIMarkupTextPrintFormatter(markupText: htmlString)
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = "test"
printInfo.outputType = .general
let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.showsNumberOfCopies = true
printController.printFormatter = formatter
let controller = UIViewController()
DispatchQueue.main.async {
printController.present(animated: true, completionHandler: { _, _, _ in
printController.printFormatter = nil
})
}
}
}
2
Answers
I find a temporary solution solution for my issue using WKWebView.
WebPrintPageRenderer.swift file
I don’t have a solution, but I also encountered this problem yesterday (what are the odds?). I just filed the following feedback report and would encourage others encountering this issues to also file a report:
My app is written in Obj-C and the code is similar to what you have above (at least in my case, I don’t think the dispatch_async is necessary, since the action is already invoked on the main queue):
The completion is never invoked (unless I hit Command-P twice):
With CG_CONTEXT_SHOW_BACKTRACE=YES I get this: