Open PDF file using swift

uidocumentinteractioncontroller pdf swift
saving pdf files with swift in ios and display them
download pdf file in swift 4
pdf viewer ios swift github
data to pdf swift
download and open pdf in swift
swift pdf
xcode swift pdf

How can I add a PDF file for an app , where you click on a button to view the file & when you're done you get back to screen you were at?

If you simply want to view a PDF file you can load it into a UIWebView.

let url : NSURL! = NSURL(string: "")
webView.loadRequest(NSURLRequest(URL: url))

Swift 4.1 :

let url: URL! = URL(string: "")
webView.loadRequest(URLRequest(url: url))

If you'd like to achieve more, a good framework is PSPDFKit.

Open PDF file using swift, to the Project. Drag and drop a document into the newly created project. In this article we’ll see how to open a pdf file using swift in iOS. Here we’ll do it with an example of opening pdf in webView in iOS. Let’s create a project and add WKWebView to the storyboard. Connect it’s outlet to the ViewController class. Now we’ll see two different things. Opening a PDF file from a URL on the web.

Apple added PDFKit framework in iOS 11

Add a UIView to your view controller and make it's class to PDFView

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet var pdfView: PDFView!

    override func viewDidLoad() {

        if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument

There are 4 display modes : singlePage, singlePageContinuous, twoUp, twoUpContinuous .

How to display PDFs using PDFView, If you simply want to view a PDF file you can load it into a UIWebView. let url : NSURL! = NSURL(string:  SWIFT 4+ If has to open file from local cache/Documentdiectory which has file path. Method 1: using UIDocumentInteractionController. class ViewController: UIViewController,UIDocumentInteractionControllerDelegate { //let path = Bundle.main.path(forResource: "Guide", ofType: ".pdf")!


If has to open file from local cache/Documentdiectory which has file path

Method 1: using UIDocumentInteractionController

class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {
   //let path =  Bundle.main.path(forResource: "Guide", ofType: ".pdf")!
    let dc = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
    dc.delegate = self
    dc.presentPreview(animated: true)
   //MARK: UIDocumentInteractionController delegates
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
     return self//or use return self.navigationController for fetching app navigation bar colour

Method 2: using WebView

  let webview = WKWebView(frame: UIScreen.main.bounds)
  webview.navigationDelegate = self
  webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL

How to open and close a PDF file using Swift?, Apple's PDFKit framework provides a huge range of code to help us work with PDFs, and one of the most useful is PDFView – it renders PDFs  Open a PDF file with Swift(PDFKit, WKWebView) PDF files are common place and with iOS/iPadOS becoming a more powerful operating system, opening PDF files and other regular computer type tasks will become more common place. In this tutorial we explore two ways of opening a PDF file with Swift.

For Xcode 8.1 and Swift 3.0

Save the PDF file in any folder of your xcode. Suppose the file name is 'Filename.pdf'

 if let pdf = Bundle.main.url(forResource: "Filename", withExtension: "pdf", subdirectory: nil, localization: nil)  {
    let req = NSURLRequest(url: pdf)
    yourWebViewOutletName.loadRequest(req as URLRequest)        

Same will apply if you want to open any html file.

Download, Store, and View PDF in Swift - David Wu, In this article we'll see how to open a pdf file using swift in iOS. Here we'll do it with an example of opening pdf in webView in iOS. Let's create a  In this article we describe how to add a PDF viewer to your iOS app using SwiftUI, Apple's declarative UI framework announced at WWDC 2019. linkGetting started. Integrating the PDFTron iOS SDK for a project that uses SwiftUI is the same as for a "standard" Swift project.

you can use UIDocumentInteractionController to preview file in IOS. In the view controller's file, add a property of type UIDocumentInteractionController

and implement a simple delegate method of it

self.documentInteractionController = UIDocumentInteractionController.init(URL: url)
self.documentInteractionController?.delegate = self

func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
    return self

don't forget to add UIDocumentInteractionControllerDelegate in view controller's class

Open PDF iOS Tutorial, To download the file from an URL, we need to use downloadTask . Since we also need to tack where the location had the task have placed our files, the observer,  Often in apps you'll be using UIDocument and other iCloud linked ways of saving files, but in the Swift blog Apple uses examples based on C and POSIX for opening and saving files. So they have in one example:

How to Add PDF Viewer to your App using Swift & PDFKit, To display an PDF file from inside the app, it can be embedded inside a Web View. In this tutorial we will display a PDF file from the local  Generate PDF file with Dynamic Data and Custom Format using HTML Template | Objective C Akash Patel In this video tutorial you can see how to save PDF file instantly with dynamic data

How to Open a PDF : Swift 3, This will make the PDFKit framework available for use in the Swift file. linkStep 2 - Implement the viewer. To display a PDF document, a PDFView  Reading & writing to a text file in Swift is simple. We use the documents directory to store our text files and read from them. The code below does this. // Save data to file let fileName = "Test" let DocumentDirURL = try!

How to open pdf file in swift 4, By the end of this video, you will be able to build a simple iOS app and able to open a PDF file! For Xcode 8. To better support Swift developers, this section will​  I want to share some files I have locally in my App using Share Sheet functionality in iPhone. I display the file in a UIWebview and when the user click the share sheet, I want to show options (email, whats app etc ) to share the file displayed on the UIWebView. I know that we can use

  • Thanks for the answer ! But is this can only be used from an external pdf, does it work if i wanted to insert pdf in the program itself ?
  • UIWebView can load a local PDF. Instead use [NSURL fileURLWithPath:strURLPath]; . . the path will be either your documents directory or your resource bundle. . There's also a method on UIWebView to load data, so you could feed it an NSData object. (Streaming from file is probably better for memory usage though).
  • If in resources directory:… (similar story for documents dir). . . build NSURL from string.
  • is PSPDFKit suggested by Jasper is free?
  • I've never tried it, but heard it is popular. On the website it looks like its a paid product, however the actual price is not clear!
  • Error inside Xcode 10.2, need to put method inside viewDidAppear()
  • You can share with iBook
  • self.documentInteractionController = UIDocumentInteractionController.init(URL: url) self.documentInteractionController?.delegate = self self.documentInteractionController?.presentPreviewAnimated(true)
  • How to close once the document is opened?