Posts

Post not yet marked as solved
0 Replies
146 Views
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:
Posted
by Halest.
Last updated
.
Post not yet marked as solved
4 Replies
429 Views
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
Posted
by Halest.
Last updated
.