MastIos
MastIos

Reputation: 123

View pdf documents

I have a table with the names of pdf documents. Previously, there were 3 documents and each one has its own ViewController. How can I make it so that with hundreds of documents, I would select one from the table and show it on the View, if I select another document, then on the same View show another document.

while I have such a function, where I substituted the name of the documents in each class and showed it in different representations. But now I need to display everything on one ViewController when selecting any document

import UIKit
import PDFKit

class pdfViewClass {


class func filePDfFunc(nameFile: String, formatFile:String, 
nameView:PDFView)
{
    if let path = Bundle.main.path(forResource: nameFile,  
ofType:formatFile) {

if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: 
path)) {

 nameView.autoScales = true
           nameView.displayDirection = .vertical
          nameView.document = pdfDocument


        }
    }
    }
}

Upvotes: 1

Views: 74

Answers (2)

Pawan Kumar
Pawan Kumar

Reputation: 558

You can use Native Apple UIDocumentInteractionController for viewing PDF file.

Create a function like below for View PDF

func viewPdf(urlPath: String, screenTitle: String) {
    // open pdf for booking id
    guard let url = urlPath.toUrl else {
        print("Please pass valid url")
        return
    }

    self.downloadPdf(fileURL: url, screenTitle: screenTitle) { localPdf in
        if let url = localPdf {
            DispatchQueue.main.sync {
                self.openDocument(atURL: url, screenTitle: screenTitle)
            }
        }
    }
}

Function for download PDF

   // method  for download pdf file
func downloadPdf(fileURL: URL, screenTitle: String, complition: @escaping ((URL?) -> Void)) {
    // Create destination URL
    if let documentsUrl: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
        let destinationFileUrl = documentsUrl.appendingPathComponent("\(screenTitle).pdf")

        if FileManager.default.fileExists(atPath: destinationFileUrl.path) {
            try? FileManager.default.removeItem(at: destinationFileUrl)
        }

        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)

        let request = URLRequest(url: fileURL)

        let task = session.downloadTask(with: request) { tempLocalUrl, response, error in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }

                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                    complition(destinationFileUrl)
                } catch let writeError {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }

            } else {
                print("Error took place while downloading a file. Error description: \(error?.localizedDescription ?? "N/A")")
            }
        }
        task.resume()
    } else {
        complition(nil)
    }
}

Function for open documents

    func openDocument(atURL url: URL, screenTitle: String) {
    self.documentInteractionController.url = url
    self.documentInteractionController.name = screenTitle
    self.documentInteractionController.delegate = self
    self.documentInteractionController.presentPreview(animated: true)
}

On tap of tableView pass the specific index URL

viewPdf(urlPath: "http://www.africau.edu/images/default/sample.pdf", screenTitle: "Tesing Document")

Upvotes: 2

Zulfikar Ali Jewel
Zulfikar Ali Jewel

Reputation: 1

You can do it using WKWebView easily. Use WKWebView to load your pdf doc.

Upvotes: 0

Related Questions