How to create and store the pdf file

I'm using CGContext.init(withUrl) to create a pdf context and doing some rendering inside that context. I was able to save the pdf in the location which will be available in the url. But in my case i will not have a location while creating the CGContext. So in my case i need a create a CGPDFContext without the location. I tried using the CGDataConsumer, but that also not working.

My Requirements
1. Have to create PDF file without the save location.
2. In certain time interval i have to save that pdf to the particular location
Answered by sAsuKe_rk in 660383022
Now i'm using the below code and it's working properly
var mediaBox = CGRect(x: 0, y: 0, width: 1000, height: 1000)
    let mutableData = CFDataCreateMutable(nil, 0)!
    let dataConsumer = CGDataConsumer.init(data: mutableData)!
    let context = CGContext.init(consumer: dataConsumer, mediaBox: &mediaBox, nil)
context?.beginPage(mediaBox: nil)
some drawing operations
context.endPage()

after this i'm getting the file location from the user and saving the pdf file that location
do {
          try (mutableData as NSData).write(to: location, options: .atomic)
        } catch {
          print(error)
        }

(I was missing the [macos] tag, so updated for macOS.)

Can you show the code which is not working?
Accepted Answer
Now i'm using the below code and it's working properly
var mediaBox = CGRect(x: 0, y: 0, width: 1000, height: 1000)
    let mutableData = CFDataCreateMutable(nil, 0)!
    let dataConsumer = CGDataConsumer.init(data: mutableData)!
    let context = CGContext.init(consumer: dataConsumer, mediaBox: &mediaBox, nil)
context?.beginPage(mediaBox: nil)
some drawing operations
context.endPage()

after this i'm getting the file location from the user and saving the pdf file that location
do {
          try (mutableData as NSData).write(to: location, options: .atomic)
        } catch {
          print(error)
        }

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()
        let consumer = CGDataConsumer(data: pdfData)!
        var mediaBox = CGRect(origin: .zero, size: size)
        let pdfContext = CGContext(consumer: consumer, mediaBox: &mediaBox, nil)!
        pdfContext.beginPage(mediaBox: &mediaBox)
        render(pdfContext)
        pdfContext.endPage()
        pdfContext.closePDF()
        return pdfData as Data
    }
}

which can be called like:

func render() -> Data {
    let size = CGSize(width: 600, height: 400)
    return CGContext.pdf(size: size) { context in
        //some drawing operations for example
        context.saveGState()
        context.setFillColor(NSColor.red.cgColor)
        context.addPath(shape.path(in: CGRect(origin: .zero, size: size)))
        context.fillPath()
        context.restoreGState()
        // end example
    }

}

And then put in a view like:

struct ContentView: View {
    var body: some View {
        Image(nsImage: NSImage(data: render())!)
            .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

or written to file like the selected answer

        let location = URL(string: "/somewhere/")!
        do {
            try (render()).write(to: location, options: .atomic)
        } catch {
            print(error)
        }

FurtherReading:

How to create and store the pdf file
 
 
Q