Greetings,
this may be an odd question but I was not able to find an answer to this.
So I am developing this pure MacOS program for a client and one thing he wants is to create some PDFs along the way. Great chance I thought to try out this fancy Swift 5 with SwiftUI and so far it's going great, most of the program is up and running but I've hit a wall with the PDF generation, which I thought would be one of the easiest parts.
Since it's a pure MacOS program, UIKit is not available but there is not a single tutorial out there for generating PDFs which is not using UIKit to do it. It seems people only want to create PDFs on iOS.
Can someone give me a small breakdown or starter or tutorial or skeleton on how I would do that?
Thanks and have a nice day
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Created
Hey everyone,
I am trying to generate a dynamically long multi-page PDF from a WKWebView with .createPDF() but the frame does not seem to make much of a difference.
Here's what I'm doing: I create the WKWebView with an A4-width and correctly calculate the height of said View, then I split up that height into A4 heights and round it up.
So let's say I get two pages.
I take that view and create frames from it with A4PageHeight*PageNumber so I get the correct starting heights of 0 for page one, 595.0 for page two etc.
Then I try to capture these frames to split up my document into multiple pages, combine them to a document and voilà.
Here's the problem.
It doesn't matter the starting height value, every capture always contains the entire Page, no matter what I do. I thought the frame would capture just a part of the page.
public func createPDFPage(pageNumber: Int, totalNumber: Int) {
numberOfPages = totalNumber
let webConfiguration = WKWebViewConfiguration()
let startHeight = CGFloat(pageNumber) * A4PageHeight
let pageWidth = A4PageWidth
let size = CGRect(x: 0, y: startHeight, width: pageWidth, height: A4PageHeight)
let webView = WKWebView(frame: size, configuration: webConfiguration)
webView.loadHTMLString(htmlString, baseURL: Bundle.main.resourceURL)
let seconds = 2.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
webView.createPDF(completionHandler: successCompletionHandler)
}
}
Results in 3 pages that look like this:
Expected Output would have looked like this:
Hello,
I had programmed and used a software for a year without problems but after updating to Monterey and updating XCode, the following, simplified Playground example of the process always fails and I cannot figure out why. Please note this project is specifically written for macOS and I cannot use the iOS libraries for PDF generation.
import WebKit
import PDFKit
import SwiftUI
import CoreGraphics
let webView: WKWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: 595.0, height: 842.0), configuration: WKWebViewConfiguration())
let htmlString = "<html><body><h1>Hello</h1></body></html>"
webView.loadHTMLString(htmlString, baseURL: nil)
webView.createPDF(completionHandler: compHandler)
func compHandler(res: Result<Data, Error>) -> Void {
print(res)
}
This always results in an error:
failure(Error Domain=WKErrorDomain Code=1 "An unknown error occurred" UserInfo={NSLocalizedDescription=An unknown error occurred})
I cannot find anything in the documentation regarding this and most examples and questions are old and not applicable (many still using Objective-C).
Debugging seems to suggest that the loadHTMLString itself fails, not even necessarily the PDF itself.
Any pointers would be appreciated.
Thanks