0

我想在 macOS 上使用WKWebView.pdf(configuration:)macOS 12/iOS 15 中引入的新功能在 macOS 上创建一个 pdf。它试图利用新的 async/await 功能(恐怕我很可能还没有完全掌握......)。

现在,我收到一个我不知道如何处理的错误:

Error Domain=WKErrorDomain Code=1 "An unknown error occurred" 
UserInfo={NSLocalizedDescription=An unknown error occurred}

我尝试将 html 字符串加载到 web 视图中,然后我想从中生成 pdf。我用来生成PDFDocument外观的函数如下所示:

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

我目前最好的猜测是WKWebView'sloadHTMLString还没有完成加载 html——我确实允许在应用程序沙箱中进行传出连接,但不是......

为了完整起见,这里是整个代码:

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("Create PDF") {
                Task {
                    await vm.generatePdf()
                }
            }
        }
    }
    
    static let initHtml = """
    <h1>Some fancy html</h1>
    <h2>…and now how do I create a pdf from this?</h2>
    """
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

class ViewModel: ObservableObject {
    @Published var html = """
    <h1>Some fancy html</h1>
    <h2>…and now let's create some pdf…&lt;/h2>
    """
    @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) -> PDFView {
        return PDFView()
    }
    
    func updateNSView(_ nsView: PDFView, context: Context) {
        nsView.document = pdfDocument
    }
}

4

0 回答 0