https://developer.apple.com/forums/thread/671947?answerId=725133022#725133022
Just because I found this answer when looking for something similar -
Putting the accepted solution into an extension on CGContext makes it handy to reuse.
extension CGContext {
static func pdf(size: CGSize, render: (CGContext) -> ()) -> Data {
let pdfData = NSMutableData()