Swift中的WKWebView印刷忽略背景
我尝试将HTML代码打印到具有以下代码的PDF上:
class PDFPrinter: NSObject, WKNavigationDelegate {
static var shared = PDFPrinter()
let webView = WKWebView()
private var saveURL: URL!
private var completion: () -> () = {}
override init() {
super.init()
self.webView.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let printInfo = NSPrintInfo(dictionary: [
.paperSize: CGSize(width: 595.28, height: 841.89),
.jobDisposition: NSPrintInfo.JobDisposition.save,
.jobSavingURL: self.saveURL!,
])
printInfo.horizontalPagination = .automatic
printInfo.verticalPagination = .automatic
let margin = 20.0
printInfo.leftMargin = margin
printInfo.topMargin = margin
printInfo.rightMargin = margin
printInfo.bottomMargin = margin
let pro = self.webView.printOperation(with: printInfo)
pro.showsPrintPanel = false
pro.showsProgressPanel = false
let selector = #selector(self.printOperationDidRun(printOperation: success: contextInfo:))
pro.runModal(for: NSWindow(), delegate: self, didRun: selector, contextInfo: nil)
}
func printHTML(htmlString: String, saveURL: URL, completion: @escaping () -> ()) {
self.saveURL = saveURL
self.completion = completion
webView.loadHTMLString(htmlString, baseURL: nil)
}
@objc func printOperationDidRun( printOperation: NSPrintOperation,
success: Bool,
contextInfo: UnsafeMutableRawPointer?){
self.completion()
}
}
此功能除了一件事:HTML的背景完全忽略了,我的PDF总是完全白色。我找不到wkwebview
,nsprintinfo
或nsprintoperation
如何更改它的设置。有什么想法吗?
I try to print html code to a pdf with the following code:
class PDFPrinter: NSObject, WKNavigationDelegate {
static var shared = PDFPrinter()
let webView = WKWebView()
private var saveURL: URL!
private var completion: () -> () = {}
override init() {
super.init()
self.webView.navigationDelegate = self
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let printInfo = NSPrintInfo(dictionary: [
.paperSize: CGSize(width: 595.28, height: 841.89),
.jobDisposition: NSPrintInfo.JobDisposition.save,
.jobSavingURL: self.saveURL!,
])
printInfo.horizontalPagination = .automatic
printInfo.verticalPagination = .automatic
let margin = 20.0
printInfo.leftMargin = margin
printInfo.topMargin = margin
printInfo.rightMargin = margin
printInfo.bottomMargin = margin
let pro = self.webView.printOperation(with: printInfo)
pro.showsPrintPanel = false
pro.showsProgressPanel = false
let selector = #selector(self.printOperationDidRun(printOperation: success: contextInfo:))
pro.runModal(for: NSWindow(), delegate: self, didRun: selector, contextInfo: nil)
}
func printHTML(htmlString: String, saveURL: URL, completion: @escaping () -> ()) {
self.saveURL = saveURL
self.completion = completion
webView.loadHTMLString(htmlString, baseURL: nil)
}
@objc func printOperationDidRun( printOperation: NSPrintOperation,
success: Bool,
contextInfo: UnsafeMutableRawPointer?){
self.completion()
}
}
This works except of one thing: The background of the html is completely ignored, my pdf is always completely white. I could not find any setting in WKWebView
, NSPrintInfo
or NSPrintOperation
how to change that. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题(似乎iOS 16.4带有类似的问题),经过数小时的尝试解决,看来该CSS指令为我解决了:
I was having the same issue (seems that iOS 16.4 comes with a similar issue) and after hours of trying to solve that, it seems that this CSS directive fixes it for me:
在 iOS 16 或以上版本编译器跳过 css 一部分您必须告诉他们这是重要
将此代码放在html文件中或在您的CSS中
in iOS 16 or above version compiler skips CSS part you have to tell them it is a IMPORTANT
Put this code in Style tag in the HTML file or in Your CSS