0

Following this example ( Swift: create pdf with WKWebView's createPdf - copied almost one to one), I try to create a pdf from WKWebView (I am still very amazed that it is still so catastrophically complicated (up to impossible) to create a PDF under macOS. (It's easier with php.)) However - code is

private func CreatePDF(htmlString: String) {
    let width = ((21/2.54)*72)
    let height = ((29.7/2.54)*72)
    if let downloadDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
        let savePath = downloadDir.appendingPathComponent("swiftPdf").appendingPathExtension("pdf")
        
        let webViewConfiguration = WKWebViewConfiguration()
        
        let webView = WKWebView(frame: .init(x: 0, y: 0, width: width, height: height), configuration: webViewConfiguration)
        
        let pdfConfiguration = WKPDFConfiguration()
        pdfConfiguration.rect = CGRect(x: 0.0, y: 0.0, width: width, height: height)

        webView.loadHTMLString(htmlString, baseURL: Bundle.main.resourceURL)
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            webView.createPDF(configuration: pdfConfiguration) { result in
                switch result {
                    case .success(let data):
                        do {
                            try data.write(to: savePath)
                            print("Successfully created and saved pdf…")
                        } catch let error {
                            print("Could not _save_ pdf: \(error)")
                        }
                    case .failure(let error): // <<-- always comes out here with error
                        print("Could not create pdf: \(error)")
                }
            }
        }
    }
}

The console tells

failure(Error Domain=WKErrorDomain Code=1 "An unknown error occurred" UserInfo={NSLocalizedDescription=An unknown error occurred.})
Could not create pdf: Error Domain=WKErrorDomain Code=1 "An unknown error occurred." UserInfo={NSLocalizedDescription=An unknown error occurred.}

Wonderful errormessage. :-)

But what ist the problem now?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mihema
  • 158
  • 1
  • 10
  • 1
    Does this help: https://stackoverflow.com/questions/70337689/create-pdf-with-wkwebview-pdfconfiguration ? – koen Aug 22 '22 at 15:44
  • try to run `createPDF` with default configuration; if it succeeds - your configuration is a problem. If it still fails - what you try to convert is a problem. For example what is `Bundle.main.resourceURL` resolving to? and does htmlString contain a valid HTML? If you display webView what does `webView.loadHTMLString(htmlString, baseURL: Bundle.main.resourceURL)` show? Does it have enough time to display a page? – timbre timbre Aug 22 '22 at 16:39
  • @koen: that's a good hint. Thanks. In combination with https://stackoverflow.com/questions/65997361/swift-create-pdf-with-wkwebviews-createpdf I could create a pdf - unfortunatelly again not paginated and text cropped at the end of the page. Hmm, weird there's no good working solution for creating pdf with all features under swift/swiftui. And I won't learn cocoa only for creating pdf while all the rest runs in swift. – mihema Aug 23 '22 at 12:35
  • And what about PDFKit? https://developer.apple.com/documentation/pdfkit – koen Aug 23 '22 at 15:30
  • PDFKit is, afaik, for manipulating pdf. I'm currently trying my hand at CGContext. Sounds promising. https://stackoverflow.com/questions/63625085/drawing-text-to-a-cgcontext-for-quartz-pdf-not-working – mihema Aug 23 '22 at 15:41

0 Answers0