title: IOS Swift generate PDF with UIWebView
body: <p>I just need to create a pdf file in my iPhone app using SWIFT to email it later.</p>
  
  <p>The pdf creates ok and display all good (Label and image), but the HTML content that should display with in the WebView does NOT render properly (just display empty WebView in white). </p>
  
  <p>following is the test code I am trying</p>
  
  <pre><code>func generatePdf() -&gt; String
  {
      var pdfLoacation: String = ""
  
      var pageSize: CGSize! = CGSize(width: 612, height: 792)         
      var fileName: String = "MySimpleDoc.pdf"
  
      var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as NSArray
      var documentDirectory = path.objectAtIndex(0) as! String
      pdfLoacation = documentDirectory.stringByAppendingPathComponent(fileName)
  
      println("Document Directory: \(pdfLoacation)")
  
      UIGraphicsBeginPDFContextToFile(pdfLoacation, CGRectZero, nil)
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil)
      var pdfContext: CGContextRef = UIGraphicsGetCurrentContext()
  
      var testView = UIView(frame: CGRectMake(0, 0, pageSize.width, pageSize.height))
      testView.backgroundColor = UIColor.yellowColor()
  
      // Adding a Label to 'testView'
      var label = UILabel(frame: CGRectMake(10, 10, 200, 20))
      label.textColor = UIColor.redColor()
      label.backgroundColor = UIColor.lightGrayColor()
      label.text = "I'am a test label"
      testView.addSubview(label)
  
  
      // PROBLEM::: [NOT DISPLAY HTML????]
      // Adding a Webview 'testView' 
      let htmlString: String = "&lt;html&gt; &lt;body&gt; &lt;h1&gt; Hello from WebView! &lt;/h1&gt; &lt;/body&gt; &lt;/html&gt;"
      var webView = UIWebView(frame: CGRectMake(10, 35, 300, 200))
      webView.loadHTMLString(htmlString, baseURL: nil)
      testView.addSubview(webView)
  
      // Adding a saved image to 'testView'
      let defaults = NSUserDefaults.standardUserDefaults()
      if let imageData = defaults.objectForKey("MyImage") as? NSData
      {
          var myImage = UIImageView(frame: CGRectMake(10, 260, 150, 100))
          myImage.image = UIImage(data: imageData)
          testView.addSubview(myImage)
      }
  
  
      // adding 'testView' to pdf context...
      testView.layer.renderInContext(pdfContext)
      UIGraphicsEndPDFContext()
      return pdfLoacation
  }
  </code></pre>
  
  <p>All I get in the pdf is as follows. </p>
  
  <p>Everything (Label and Image) display as I expect them to be, but the WebView does not render it's HTML content (h1 tag content - "Hello from WebView!") at all... Instead just display the empty WebView in white colour...</p>
  
  <p><a href="https://i.stack.imgur.com/mgFwb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mgFwb.png" alt="enter image description here"></a></p>