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)
}