4

我在从 UIWebView 创建 PDF 时遇到问题。

        let render = UIPrintPageRenderer();      
        /* 1. pdf from webView */
        render.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAtIndex: 0);

        // 2. Assign paperRect and printableRect

        let page = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4, 72 dpi */

        let printable = CGRectInset(page, 0, 0)

        render.setValue(NSValue(CGRect: page), forKey: "paperRect")
        render.setValue(NSValue(CGRect: printable), forKey: "printableRect")

        // 3. Create PDF context and draw

        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil)


        for i in 1...render.numberOfPages() {

            UIGraphicsBeginPDFPage();
            let bounds = UIGraphicsGetPDFContextBounds()
            render.drawPageAtIndex(i - 1, inRect: bounds)
        }

        UIGraphicsEndPDFContext();

        // 4. Save PDF file
        print("open \(path)");
        pdfData.writeToFile(path, atomically: true);

一切正常,但如果我在 HTML 中使用背景图片,它就会消失。有谁知道怎么了?

4

1 回答 1

5

首先,您需要创建您的 html 代码:

var html = "<html><head><meta charset='UTF-8'></head><body>hello from html </body></html>"

比您需要使用以下方法实例化您的 Web 视图:

let webView = UIWebView(frame: self.view.bounds)
self.view.addSubview(webView)
webView.delegate = self
webView.loadHTMLString(html, baseURL: nil)
// webView.isUserInteractionEnabled = true

之后你需要让你的 ViewController 类实现UIWebViewDelegate并实现该方法,最后在方法func webViewDidFinishLoad(_ webView: UIWebView) 中添加以下代码webViewDidFinishLoad

func webViewDidFinishLoad(_ webView: UIWebView) {

    let render = UIPrintPageRenderer()
    render.addPrintFormatter(webView.viewPrintFormatter(), startingAtPageAt: 0);


    let page = CGRect(x: 0, y: 10, width: webView.frame.size.width, height: webView.frame.size.height) // take the size of the webView
    let printable = page.insetBy(dx: 0, dy: 0)
    render.setValue(NSValue(cgRect: page), forKey: "paperRect")
    render.setValue(NSValue(cgRect: printable), forKey: "printableRect")

    // 4. Create PDF context and draw
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)
    for i in 1...render.numberOfPages {

        UIGraphicsBeginPDFPage();
        let bounds = UIGraphicsGetPDFContextBounds()
        render.drawPage(at: i - 1, in: bounds)
    }
    UIGraphicsEndPDFContext();
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

    print(documentsPath)

    pdfData.write(toFile: "\(documentsPath)/pdfName.pdf", atomically: true)

    self.pdfPath = "\(documentsPath)/pdfName.pdf"
    self.pdfTitle = "pdfName"
    self.performSegue(withIdentifier: "showPDFSegue", sender: nil)
    webView.removeFromSuperview()
    self.loadingScreenViewController.view.removeFromSuperview()
}

所以要恢复逻辑:要从 webView 生成 pdf,您需要准备 html 代码,创建 WebView 并在 webView 中注入 html 代码,然后在 webView didFinishLoading 中使用相同的宽度和高度打印 pdf 中的 html 内容网络视图。

于 2017-03-15T11:14:07.313 回答