Multipage PDF with PDFKit on macOS with swift

Hello everybody,

After weeks and months of searching for a solution (with interruptions) I finally have to ask the question: is there a tutorial to generate PDF with multiple pages from a macOS app with swift / swiftui?

I am programming a macOS app and want (must) save and print letters and other documents as PDFs at the touch of a button. All the examples that I have been able to find so far are only for iOS and simply turning "UIGraphicsPDFRenderer" into "NSGraphicsPDFRenderer" is not possible.

It can't be that no PDF can be created under macOS. It has to work somehow, doesn't it?

Alternatively, it would be enough for me if you could print a view from SwiftUI. But that doesn't seem to work that easily, too.

I would be very grateful for your help.

There are two ways to create PDFs on Mac. The first is to use NSPrintOperation, which is in the AppKit framework. The second is to use the Quartz framework. If you have a lot of text, using Core Text with Quartz may help.

The following article shows what you need to do to create a PDF with Quartz and Core Text:

https://www.meandmark.com/blog/2016/08/creating-pdfs-with-core-text-and-quartz/

Multipage PDF with PDFKit on macOS with swift
 
 
Q