【问题标题】:issue while preview pdf from remoteURL从 remoteURL 预览 pdf 时出现问题
【发布时间】:2020-02-17 06:05:58
【问题描述】:

我正在我的Swift 应用程序中实现 pdf 预览,所以我决定使用第三方库来预览我在下面的库中使用的 PDF

Please Check Library Here

所以首先我下载 url 并存储到文档目录,然后显示它,但下面没有预览的 pdf 是我的代码

func downloadFileFromURL(url: String)  {

    if let audioUrl = URL(string: url) {

        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
        print(destinationUrl)


        if FileManager.default.fileExists(atPath: destinationUrl.path) {
            print("The file already exists at path")
            print(destinationUrl)
            let document = try! PDFDocument(filePath: destinationUrl.lastPathComponent, password: "")

            self.collectionView.document = document
        } else {
            URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in
                guard let location = location, error == nil else { return }
                do {
                    try FileManager.default.moveItem(at: location, to: destinationUrl)
                    print(destinationUrl)
                    let document = try! PDFDocument(filePath: destinationUrl.lastPathComponent, password: "")

                    self.collectionView.document = document
                    print("File moved to documents folder")
                } catch let error as NSError {
                    print(error.localizedDescription)
                }
            }).resume()
        }
    }
}

viewDidLoad() 内部,我正在执行下面的代码

downloadFileFromURL(url: "http://housedocs.house.gov/edlabor/AAHCA-BillText-071409.pdf")

但仍然没有预览 pdf 有人可以告诉我使用 UXMPdf 预览 pdf 的正确方法

或者建议我最好的 pdfviewer for Swift 从中我可以从 URL 加载 pdf

【问题讨论】:

    标签: ios swift pdf pdfview


    【解决方案1】:

    您必须指定完整的path 而不是最后一个路径组件。

    并删除 do - catch 块内的 !

    let document = try PDFDocument(filePath: destinationUrl.path, password: "")
    

    罢工>

    由于 password 参数未使用,我建议使用内置初始化程序

    let document = try PDFDocument(url: destinationUrl)
    

    【讨论】:

    • 如果我输入 url 比我得到错误 Incorrect argument label in call (have 'url:', expected 'coder:')
    • 似乎库隐藏了url 初始化程序。所以使用第一种语法。
    • 您是否替换了PDFDocument(filePath: 的两次出现?设置断点以检查文档是否完全加载
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2021-03-28
    相关资源
    最近更新 更多