Skip to content Skip to sidebar Skip to footer

Uimarkuptextprintformatter Never Renders Base64 Images

Im creating a pdf file out of html content in swift 3.0: /** * */ func exportHtmlContentToPDF(HTMLContent: String, filePath: String) { // let webView = UIWebView(frame: CGRec

Solution 1:

This happens because WebKit first parses the HTML into a DOM, and renders content on multiple event loop cycles. You therefore need to wait for not just the page DOM to be ready but for the resource loading to be complete. As you also suggest, you need to refactor your code such that the webview gets loaded first, and you only then export its contents.

To determine the correct time to fire the export, you can observe for the state of the DOM document in the web view. There are multiple ways to do this, but the most readable option I find is a port of an answer to a related Objective-C question: in your UIWebViewDelegate implementation, implement webViewDidFinishLoad in the following way to monitor document.readyState:

funcwebViewDidFinishLoad(_webView: UIWebView) {

        guardlet readyState = webView.stringByEvaluatingJavaScript(from: "document.readyState"),
            readyState =="complete"else
        {
            // document not yet parsed, or resources not yet loaded.return
        }

        // This is the last webViewDidFinishLoad call --> export.//// There is a problem with this method if you have JS code loading more content:// in that case -webViewDidFinishLoad can get called again still after document.readyState has already been in state 'complete' once or more.self.exportHtmlContentToPDF()
    }

Solution 2:

I found the solution!

The export to PDF happens before the rendering process is finished. If you put in a very small picture it is showing up in the PDF. If the picture is too big the rendering process takes too much time but the PDF export isnt waiting for the rendering to finish.

So what I did to make it work is the following:

Before I export to PDF I show the Result of the HTML in a WebView. The WebView is rendering everything correctly and now when I press on export to PDF the PDF is showing up correctly with all images inside.

So I guess this is a huge lag that there is no way to tell the PDF Exporter to wait for the rendering process to finish.

Post a Comment for "Uimarkuptextprintformatter Never Renders Base64 Images"