I'm trying to generate a pdf using swift. The following code compiles without error, however, the "xp.pdf" document is never created. Your help is much appreciated.
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func generatePDF(sender: AnyObject) {
let pageSize:CGSize = CGSizeMake (850, 1100)
let fileName: NSString = "xp.pdf"
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)
generatePDFs(pdfPathWithFileName)
}
func generatePDFs(filePath: String) {
UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil)
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 850, 1100), nil)
drawBackground()
UIGraphicsEndPDFContext()
}
func drawBackground () {
let context:CGContextRef = UIGraphicsGetCurrentContext()
let rect:CGRect = CGRectMake(0, 0, 850, 1100)
CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
CGContextFillRect(context, rect)
}