查看pdf文档

View pdf documents

我有一个 table 有 pdf 文档的名称。以前,有 3 个文档,每个文档都有自己的 ViewController。我怎样才能做到这样,对于数百个文档,我会 select 来自 table 的一个并将其显示在视图上,如果我 select 另一个文档,则在同一个视图上显示另一个文档。

虽然我有这样的功能,但我在每个 class 中替换了文档的名称,并以不同的表示形式显示了它。但是现在我需要在 selecting 任何文档

时在一个 ViewController 上显示所有内容
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


        }
    }
    }
}

您可以使用 WKWebView 轻松完成。使用 WKWebView 加载您的 pdf 文档。

您可以使用 Native Apple UIDocumentInteractionController 查看 PDF 文件。

为查看 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)
            }
        }
    }
}

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)
    }
}

打开文档的函数

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

点击 tableView 传递特定索引 URL

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