How to show PDF from NSData in Swift - how to save PDF to Swift docs folder - how to display PDF from saved NSData via WebView in Swift

I have a PDF file that I am extracting from a section that came in the form of NSData, and now I need to display it. I looked far and wide to resolve this issue, and I did not find anything that would bridge the gap between the raw NSData and actually save or display the PDF.

I tried this, but now I'm stuck, not knowing how to save or show:

let cfData = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length) let cgDataProvider = CGDataProviderCreateWithCFData(cfData) let cgPDFDocument = CGPDFDocumentCreateWithProvider(cgDataProvider) 
+6
source share
3 answers

First of all, NSData is your PDF file, there is no need to convert it to another data type or use the library to manage it at the moment.

Now just save the NSData in the document directory on iOS, which is not a managed persistent storage such as CoreData or Realm, and is not UserDefaults. This is a folder with things in it.

Save to documents folder:

 let data = //the stuff from your web request or other method of getting pdf let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] let filePath = "\(documentsPath)/myCoolPDF.pdf" data.writeToFile(filePath, atomically: true) 

Now check if there is a file and open it on your Mac. This is to ensure that you save the actual PDF file, not something else. This is a two-step process. First, find out where, in fact, the file went by printing the location of the document folder from the iOS simulator:

 let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = paths[0] print(documentsDirectory) 

Now copy this really long path to the file and cd [paste location] to the terminal to go there, then open myCoolPDF.pdf to open it in Preview! These are magical times!

Now that you have confirmed that you are dealing with an actual PDF file, it’s time to display it in a WebView, as it’s pretty simple if you don’t have your own way of doing it.

Note that you still need to make the web view appear, drag it onto your viewController into the storyboard and make it an IBOutlet.

 let url = NSURL(fileURLWithPath: filePath) let webView = UIWebView() webView.loadRequest(NSURLRequest(URL: url)) 

Obviously, this is a quick way to make sure that you are based on the basic principles, you don’t want to force the deployment to be disabled using ! .

+11
source

Update:

  • Xcode 8.1
  • Swift 3.0.1

To save files to the document directory.

 let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let path = "\(documentPath)/filename.pdf" yourData.write(toFile: path, atomically: true) 

To get the path to the directory of documents.

 let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] 

Here you can add your backslash file name in front. And you can check if the file exists there.

+1
source

Starting with iOS 11, you don’t need web browsing and you can use the dedicated PDFKit infrastructure. However, loading an instance of a PDF URL or Data can be as simple as:

 import PDFKit // some other code here // ... let pdfView = PDFView() // add pdfView to the view hierarchy and possibly add auto-layout constraints // ... pdfView.document = PDFDocument(data: data) 

Learn more about PDFKit documentation .

+1
source

All Articles