5

I want to create pdf in document directory and want to give page numbers so I need CGPDFDocumentRef object.

let fileName: NSString = "test.pdf"

let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

let documentDirectory: AnyObject = path.objectAtIndex(0)

let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)

UIGraphicsBeginPDFContextToFile(pdfPathWithFileName as String, CGRectZero, nil)

let ref : CGContextRef = UIGraphicsGetCurrentContext()!

let localUrl = NSURL.fileURLWithPath(pdfPathWithFileName)

I have converted file path in url but this below line generates a crash and I don't know why..?

let pdfDocumentRef: CGPDFDocumentRef = CGPDFDocumentCreateWithURL(localUrl as CFURLRef)!
halfer
  • 19,824
  • 17
  • 99
  • 186
Anil Kukadeja
  • 1,348
  • 1
  • 14
  • 27
  • Do you have any output from the crash? Also, try extracting the `localUrl as CFURLRef` to a separate line and see if that is `nil` by any chance, I'm guessing that this is where you problem lies. – pbodsk May 04 '16 at 07:36
  • @pbodsk : Yes it returns nil and I don't know why..? this line returns CGPDFDocumentCreateWithURL(localUrl as CFURLRef)! nil. Although localUrl has a file path. – Anil Kukadeja May 04 '16 at 07:42
  • In Swift, you don't need to explicitly name the type that a variable is if Swift can infer the type. For instance, `let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)` can easily be represented as `let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)`, with the added bonus of Swift knowing the types used by the array is a `String`. – MaddTheSane May 04 '16 at 17:15

3 Answers3

0

Its because your test.pdf is not present into document directory folder.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0
 let localUrl  = pdfPathWithFileName as CFStringRef
 let pdfDocumentRef = CFURLCreateWithFileSystemPath(nil, localUrl, CFURLPathStyle.CFURLPOSIXPathStyle, false)
 let page_count = CGPDFDocumentGetNumberOfPages(pdfDocumentRef)
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

So it seems your NSURL(fileURLWithPath: pdfPathWithFileName) as CFURLRef part fails and that is what is causing your problems.

I just found this in the documentation about CFURL

CFURL fails to create an object if the string passed is not well-formed (that is, if it does not comply with RFC 2396). Examples of cases that will not succeed are strings containing space characters and high-bit characters. If a function fails to create a CFURL object, it returns NULL, which you must be prepared to handle. If you create CFURL objects using file system paths, you should use the CFURLCreateFromFileSystemRepresentation and CFURLCreateFromFileSystemRepresentationRelativeToBase functions, which handle the subtle differences between URL paths and file system paths.

And this question seems to be about the same problem. As one of the answers suggest:

You need the convert NSString to CFStringRef then call CFURLCreateWithFileSystemPath;

Hope that helps you.

Community
  • 1
  • 1
pbodsk
  • 6,787
  • 3
  • 21
  • 51