0

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)
    }
Celeo
  • 5,583
  • 8
  • 39
  • 41
Tom
  • 790
  • 2
  • 9
  • 32
  • Update: code above does work - I was looking for file in wrong directory! – Tom Nov 21 '14 at 17:10
  • Hi I want to Show attributed text in PDF file in Swift . Please Suggest What Changes Will required In Obove code for Show Attibuted text in swift – jaydev Apr 06 '16 at 12:28
  • A good summary of the available options: https://pspdfkit.com/blog/2018/ways-to-create-a-pdf-on-ios/ – steipete May 23 '18 at 20:52

2 Answers2

1

Do you have a pdfkit in the place? PSPDFKit works pretty well from what I understand. You just need to download it from pspdfkit.com, then drag and drop the PSPDFKit.framework into the "Embedded Binaries" section of your app's target. You do have to pay for it though, so theres that. I think there is a free trial for it though, so you can try it out and see if thats what you need or want.

Ronn Weasley
  • 71
  • 1
  • 6
0

For Xcode 7 (swift 2.0) I had to make the following changes to get it to work:

replace:

let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName)

with:

let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)

and unwrap the UIGraphicsGetCurrentContext():

let context:CGContextRef = UIGraphicsGetCurrentContext()!

and also you can remove:

let pageSize:CGSize = CGSizeMake (850, 1100)

because you never use pageSize

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
brianbird
  • 9
  • 1