Swift中的WKWebView印刷忽略背景

发布于 2025-02-04 17:35:00 字数 1897 浏览 4 评论 0原文

我尝试将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总是完全白色。我找不到wkwebviewnsprintinfonsprintoperation如何更改它的设置。有什么想法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2025-02-11 17:35:00

我遇到了同样的问题(似乎iOS 16.4带有类似的问题),经过数小时的尝试解决,看来该CSS指令为我解决了:

@media print {
* {
    -webkit-print-color-adjust: exact !important;
  }
}

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:

@media print {
* {
    -webkit-print-color-adjust: exact !important;
  }
}
压抑⊿情绪 2025-02-11 17:35:00

iOS 16 或以上版本编译器跳过 css 一部分您必须告诉他们这是重要

将此代码放在html文件中或在您的CSS中

<style>
    @media print {
    * {
        -webkit-print-color-adjust: exact !important;
      }
    }
</style>

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

<style>
    @media print {
    * {
        -webkit-print-color-adjust: exact !important;
      }
    }
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文