title: Create pdf with WKWebView.pdf(configuration:)
body: <p>I want to create a pdf on macOS with the new <code>WKWebView.pdf(configuration:)</code> which was introduced in macOS 12/iOS 15. It tries to make use of the new async/await functionality (which I most likely have not grasped entirely I am afraid...).</p>
  <p>Right now, I am getting an error that I have no idea how to handle:</p>
  <pre><code>Error Domain=WKErrorDomain Code=1 &quot;An unknown error occurred&quot; 
  UserInfo={NSLocalizedDescription=An unknown error occurred}
  </code></pre>
  <p>I try to load a html string into a web view, which I then want to generate the pdf from. The function I use to generate my <code>PDFDocument</code> looks like this:</p>
  <pre class="lang-swift prettyprint-override"><code>func generatePdf() async {
          let webView = WKWebView()
          await webView.loadHTMLString(html, baseURL: nil)
          
          let config = WKPDFConfiguration()
          config.rect = .init(origin: .zero, size: .init(width: 595.28, height: 841.89))
          do {
              //this is where the error is happening
              let pdfData = try await webView.pdf(configuration: config)
              self.pdf = PDFDocument(data: pdfData)
          } catch {
              print(error) //this error gets printed
          }
      }
  </code></pre>
  <p>My best guess as it currently stands is that <code>WKWebView</code>'s <code>loadHTMLString</code> has not finished loading the html–I did allow for outgoing connection in the app sandbox that's not it...</p>
  <p>For the sake of completeness, here's the entire code:</p>
  <pre class="lang-swift prettyprint-override"><code>import SwiftUI
  import PDFKit
  import WebKit
  
  @main
  struct AFPdfApp: App {
      var body: some Scene {
          WindowGroup {
              ContentView()
          }
      }
  }
  
  struct ContentView: View {
      @StateObject private var vm = ViewModel()
      
      var body: some View {
          VStack {
              TextEditor(text: $vm.html)
                  .frame(width: 300.0, height: 200.0)
                  .border(Color.accentColor, width: 1.0)
                  .padding()
              PdfViewWrapper(pdfDocument: $vm.pdf)
          }
          .toolbar {
              Button(&quot;Create PDF&quot;) {
                  Task {
                      await vm.generatePdf()
                  }
              }
          }
      }
      
      static let initHtml = &quot;&quot;&quot;
      &lt;h1&gt;Some fancy html&lt;/h1&gt;
      &lt;h2&gt;…and now how do I create a pdf from this?&lt;/h2&gt;
      &quot;&quot;&quot;
  }
  
  struct ContentView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  
  class ViewModel: ObservableObject {
      @Published var html = &quot;&quot;&quot;
      &lt;h1&gt;Some fancy html&lt;/h1&gt;
      &lt;h2&gt;…and now let's create some pdf…&lt;/h2&gt;
      &quot;&quot;&quot;
      @Published var pdf: PDFDocument? = nil
      
      func generatePdf() async {
          let webView = WKWebView()
          await webView.loadHTMLString(html, baseURL: nil)
          
          let config = WKPDFConfiguration()
          config.rect = .init(origin: .zero, size: .init(width: 595.28, height: 841.89))
          do {
              let pdfData = try await webView.pdf(configuration: config)
              self.pdf = PDFDocument(data: pdfData)
          } catch {
              print(error)
          }
      }
  }
  
  struct PdfViewWrapper: NSViewRepresentable {
      @Binding var pdfDocument: PDFDocument?
      
      func makeNSView(context: Context) -&gt; PDFView {
          return PDFView()
      }
      
      func updateNSView(_ nsView: PDFView, context: Context) {
          nsView.document = pdfDocument
      }
  }
  
  </code></pre>