0

Iv used the following Code from Tom to create a Pdf File and

PDF Generation Swift

im having the same problem finding the document I created using Tom's code. Iv search my Mac HD and still no document and the programme does not crash. And when i look at the log it shows it has been stored at the following path

pdfPathWithFileName String "/Users/cleota/Library/Developer/CoreSimulator/Devices/B72A97D0-339E-4E79-AED6-4991D3B7C4B7/data/Containers/Data/Application/7A7789C4-27C5-45FB-8604-E36865609764/Documents/Ezy Essay Test.pdf"

but when I go there these folders(Library/Developer/CoreSimulator/Devices)are not there where it says it has been stored. Thoughts please?

    @IBAction func createPdfDocument(sender: AnyObject) {

    let fileName: String = "Taumaoe.pdf"
    let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    let documentDirectory: AnyObject = path.objectAtIndex(0)
    let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)

            print(pdfPathWithFileName)

    generatePdf(pdfPathWithFileName)

}

func generatePdf(filepath: String){

    UIGraphicsBeginPDFContextToFile(filepath, CGRectZero, nil)
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 850, 1100), nil)
    drawBackground()

}

func drawBackground(){

    var context:CGContextRef = UIGraphicsGetCurrentContext()!

   // var rect:CGRect = CGRectMake(0, 0, CGSize.height, CGSize.width)
    //CGContextSetFillColorWithColor(context, UIColor.greenColor().CGColor)
    //CGContextFillRect(context, rect)

    var testView = UIView (frame: CGRectMake(0,0,850,1100))
    testView.backgroundColor = UIColor.yellowColor()

    var label = UILabel (frame: CGRectMake(10,10,100,20))
    label.textColor = UIColor.redColor()
    label.backgroundColor = UIColor.lightGrayColor()
    label.text = "I'am a test label"
    testView.addSubview(label)

    testView.layer.renderInContext(context)

    UIGraphicsEndPDFContext()

    //Title of 1st option of MainController

            let sexyKeyWordscontroller = UIAlertController(title: "Ezy Essay Saved!", message: "Well done! Your Ezy Essay Pdf document has been saved at the following path: ", preferredStyle: UIAlertControllerStyle.Alert)

            let closeButton : UIAlertAction = UIAlertAction(title: "Close",
                style: UIAlertActionStyle.Cancel,
                handler: {
                    (alert: UIAlertAction!) in sexyKeyWordscontroller.dismissViewControllerAnimated(true,
                        completion: nil)
            })


            sexyKeyWordscontroller.addAction(closeButton)

            self.presentViewController(sexyKeyWordscontroller, animated: true, completion: nil)


}

Thanks

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Charlie
  • 170
  • 12
  • Can you show the way you store the file here? and probably the path? – Lee Mar 14 '16 at 04:26
  • Hey Lee, i have updated my post with the following code I used to create a mock pdf. Hope it makes sense. thanks. – Charlie Mar 14 '16 at 19:31
  • This is the way I currently use, it is pretty straight forward. You can check and use it, just replace the html content will do the trick. [Generate PDF](https://gist.github.com/nyg/b8cd742250826cb1471f) – Lee Mar 15 '16 at 01:02
  • Thanks Lee, Still not working. Man do you know if I need a permission to save documents to external storage or does apple do this by default? Also do I need to create the file path manually or is the file path created by default and its something that should exists when I run the iphone5 simulator? – Charlie Mar 15 '16 at 02:45
  • The file path is correct, and if you are using simulator it should be a folder there with the same name as mention in @Rashwan's answer. If you really not sure, then simple try write simple text to the path with `.txt` extention and try instead of doing pdf. `simpleString.writeToPath(pdfPathWithFileName, atomically: false, encoding: NSUTF8StringEncoding, error: nil)` – Lee Mar 15 '16 at 03:03
  • And as Rashwan's said, use the filePath you get to find the folder, being with `~/` and followed by `Library` onward until `Documents`. The file path actually mean different simulator code, and also different apps in that simulator, so you need to check the correct one to open or else there will be nothing inside – Lee Mar 15 '16 at 03:06
  • Yep it worked. Man was that layers of folders. Did it with the search bar at the top of the computer screen and it worked but didnt for the finder search bar, why is that?. Thanks guys for your help...awesome work!. Took me ages... – Charlie Mar 15 '16 at 19:13

1 Answers1

1

If you print out pdfPathWithFileName in the button event you will get the file path. When you have the file path open a finder window and in the file menu click: Go > Go to folder

The path that you have printed out will look something like this:

/Users/USERNAME/Library/Developer/CoreSimulator/Devices/...

Copy this into the Go to folder from the path

~/Library/Developer/CoreSimulator/Devices/...

And of course skip the last "/xp.pdf" in the path if you only want to open the location of the folder and not the file itself.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • Hey Rashwan, tried it but still no file path. I wrote in print(pdfPathWithFileName) as you instructed but still nothing. Wonder if I need to add anything to the simulator or maybe to my app for there to be a permission or something for the app to be able to write to the phone storage so the file path can be created. Thoughts? I will update my code so you can see where I put the print(pdfPathWithFileName). – Charlie Mar 14 '16 at 19:46
  • 1
    Thanks Rashwan, finally found it. Doesnt show up in the finder search bar but does so in the search bar at the top of my mac. Why is that?... thanks anyway.. lifesaver... – Charlie Mar 15 '16 at 19:14
  • That's weird. Glad it helped though. – Rashwan L Mar 15 '16 at 20:06
  • Rashwan L sorry but was wondering if you know if I can attach this PDF file I created to as an email intent in swift? Am struggling to work out a solution to get this pdf doc to the user. In android you can access it via the external hard drive but in IOS you cant access this unless you have other apps to open the files. Hope that makes sense.. any ideas anyone please? – Charlie Apr 02 '16 at 02:01
  • Rashwan, I did check it out and used the code and hence I posted a new question here... http://stackoverflow.com/questions/36404234/pdf-document-wont-attach-to-ios-email?noredirect=1#comment60440647_36404234 so just waiting to see if anyone has any ideas why its not attaching.. – Charlie Apr 06 '16 at 06:30