Getting Your View to Generate PDF Data

// MARK: - Getting Your View to Generate PDF Data
@IBAction func savePDF(sender: AnyObject!) {
    let savePanel = NSSavePanel()
    savePanel.allowedFileTypes = ["pdf"]
    savePanel.beginSheetModal(for: window!, completionHandler: {
        [unowned savePanel] (result) in
        if result == NSApplication.ModalResponse.OK {
            let data = self.dataWithPDF(inside: self.bounds)
            do {
                try data.write(to: savePanel.url!, options: NSData.WritingOptions.atomic)
            } catch let error as NSError? {
                let alert = NSAlert(error: error!)
                alert.runModal()
            }
        }
    })
}