Saving WebView to PDF returns blank image?

匿名 (未验证) 提交于 2019-12-03 02:52:02

问题:

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

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)         }     }  } 

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

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

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

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?

Any ideas?

回答1:

So I kind of figured out how to solve it, it turns out you can't (especially on OSX) access and print a webview from a WKWebView.

You have to use a WebView and NOT a WKWebView (I originally started with WKWebView because a few of the articles I read said to use that).

A WebView object is pretty much similar to a WKWebView object, which is fun as hell :-)

But it gives you access to .mainFrame & .frameView which you'll need to print it's content.

Here's my code:

    let webView = WebView(frame: self.view.frame)     let localfilePath = NSBundle.mainBundle().URLForResource(fileName, withExtension: "html");     let req = NSURLRequest(URL: localfilePath!);     webView.mainFrame.loadRequest(req)     self.view.addSubview(webView) 

Once it's rendered I then added a 1 second delay just to make sure the content has rendered before I print it,

    // needs 1 second delay     let delay = 1 * Double(NSEC_PER_SEC)     let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))     dispatch_after(time, dispatch_get_main_queue()) {         // works!         let data = webView.dataWithPDFInsideRect(webView.frame)         let doc = PDFDocument.init(data: data)         doc.writeToFile("/Users/john/Desktop/test.pdf")          // works!         let printInfo = NSPrintInfo.sharedPrintInfo()         let printOperation = NSPrintOperation(view: webView.mainFrame.frameView, printInfo: printInfo)         printOperation.runOperation()     } 

Here I'm printing it and saving it as a PDF, just so I'm doubly sure it works in all circumstances.

I'm sure it can be improved, I hate the delay hack, should replace that with some kind of callback or delegate to run when the content has fully loaded.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!