将 WebView 保存为 PDF 会返回空白图像吗? [英] Saving WebView to PDF returns blank image?

查看:34
本文介绍了将 WebView 保存为 PDF 会返回空白图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何将 WebView 保存为 PDF 并且完全卡住了,真的很感激一些帮助吗?

I'm trying to figure out how to save a WebView to a PDF and totally stuck, would really appreciate some help?

我在 Cocoa &OSX 上的 Swift,这是我目前的代码:

I'm doing this in Cocoa & Swift on OSX, here's my code so far:

import Cocoa
import WebKit

class ViewController: NSViewController {

    override func loadView() {
        super.loadView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        loadHTMLString()
    }

    func loadHTMLString() {
        let webView = WKWebView(frame: self.view.frame)
        webView.loadHTMLString("<html><body><p>Hello, World!</p></body></html>", baseURL: nil)
        self.view.addSubview(webView)
        createPDFFromView(webView, saveToDocumentWithFileName: "test.pdf")
    }

    func createPDFFromView(view: NSView, saveToDocumentWithFileName fileName: String) {
        let pdfData = view.dataWithPDFInsideRect(view.bounds)
        if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
            let documentsFileName = documentDirectories + "/" + fileName
            debugPrint(documentsFileName)
            pdfData.writeToFile(documentsFileName, atomically: false)
        }
    }

}

这很简单,我正在做的是创建一个 WebView 并向其写入一些基本的 html 内容来呈现:

It's pretty simple, what I'm doing is creating a WebView and writing some basic html content to it which renders this:

然后获取视图并将其保存为 PDF 文件,但结果为空白:

And then takes the view and saves it to a PDF file but that comes out blank:

我尝试从 webView 和 View 中抓取内容,但没有任何乐趣.

I've tried grabbing the contents from the webView and View but no joy.

我在这里发现了类似的问题 如何在 webview 完成渲染时截取屏幕截图关于将 webview 保存到图像,但到目前为止没有使用 OSX 解决方案.

I've found a similar problem here How to take a screenshot when a webview finished rending regarding saving the webview to an image, but so far no luck with an OSX Solution.

这可能与文档尺寸有关吗?或者内容在子视图中?也许如果您捕获视图,您就无法捕获子视图?

Could it be something to do with the document dimensions? or that the contents is in a subview? maybe if you capture the View you can't capture the SubView?

有什么想法吗?

推荐答案

iOS 11.0 及以上,Apple 提供了以下 API 来捕获 WKWebView 的快照.

iOS 11.0 and above, Apple has provided following API to capture snapshot of WKWebView.

@available(iOS 11.0, *)
    open func takeSnapshot(with snapshotConfiguration: WKSnapshotConfiguration?, completionHandler: @escaping (UIImage?, Error?) -> Swift.Void)

示例用法:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        if #available(iOS 11.0, *) {
            webView.takeSnapshot(with: nil) { (image, error) in
                //Do your stuff with image
            }
        }
    }

iOS 10 及更低版本,必须使用 UIWebView 来捕获快照.可以使用以下方法来实现.

iOS 10 and below, UIWebView has to be used to capture snapshot. Following method can be used to achieve that.

func webViewDidFinishLoad(_ webView: UIWebView) {

        let image = captureScreen(webView: webView)
        //Do your stuff with image
    }

func captureScreen(webView: UIWebView) -> UIImage {
        UIGraphicsBeginImageContext(webView.bounds.size)
        webView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }

这是另一个相关的答案

这篇关于将 WebView 保存为 PDF 会返回空白图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆