gpt4 book ai didi

ios - 如何在 PDFKit 中显示从互联网下载的 pdf?

转载 作者:行者123 更新时间:2023-11-28 11:24:54 30 4
gpt4 key购买 nike

我已经成功地从网上下载了 pdf 文件,它保存在 documents 目录中。下载文件的url如下

file:///Users/heetshah/Library/Developer/CoreSimulator/Devices/4BF83AAF-A910-46EB-AE76-91BC6BEED033/data/Containers/Data/Application/B4532805-2842-431F-B16C-C5E448C8366F/Documents/TPAFForm.pdf

我正在尝试按如下方式将其显示到 PDFKit。

let path = URL(fileURLWithPath: pdfUrl!)
if let document = PDFDocument(url: path) {
pdfView.document = document
pdfView.displayMode = .singlePageContinuous
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
pdfView.displaysAsBook = true
pdfView.displayDirection = .vertical
pdfView.autoScales = true
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}

我没有收到任何错误

我浏览了一堆堆栈溢出帖子,它显示的解决方案与上面相同,但它不适用于我的情况。我也尝试了以下解决方案,但它不起作用

if let path = Bundle.main.path(forResource: pdfUrl, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {..

以下是我下载文件的代码

func downloadPDF(pdfUrl: String?,fileName: String,completionHandler:@escaping(String,Bool) -> ()){
let destinationPath: DownloadRequest.DownloadFileDestination = {
_,_ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("\(fileName).pdf")
return (fileURL,[.removePreviousFile,.createIntermediateDirectories])
}

if let pdfUrl = pdfUrl {
Alamofire.download(pdfUrl, to: destinationPath).downloadProgress { (progress) in

}.responseData { (response) in
switch response.result{
case .success:
if response.destinationURL != nil,let filePath = response.destinationURL?.absoluteString{
completionHandler(filePath,true)
}
break
case .failure:
completionHandler("Something went wrong",false)
break
}
}
}
}

我正在使用 Alamofire 下载文件。我的 PDFView 的约束是正确的,因为我能够在预览中显示在线 url pdf,但我需要先在本地下载 pdf,然后在我的 pdf View 中显示它

最佳答案

由于您没有显示足够的代码来调试问题,这里是完整代码来执行您描述的操作,可以通过将您的代码与我的:

import UIKit
import PDFKit

class ViewController: UIViewController {

let pdfurl = URL(string:"https://www.apeth.com/rez/release.pdf")!
let pdffileurl : URL = {
let fm = FileManager.default
let docsurl = try! fm.url(
for: .documentDirectory, in: .userDomainMask,
appropriateFor: nil, create: true)
return docsurl.appendingPathComponent("mypdf.pdf")
}()

override func viewDidLoad() {
super.viewDidLoad()
let sess = URLSession.shared
sess.downloadTask(with: self.pdfurl) { (url, resp, err) in
if let url = url {
let fm = FileManager.default
try? fm.removeItem(at: self.pdffileurl)
try? fm.moveItem(at: url, to: self.pdffileurl)
DispatchQueue.main.async {
self.displayPDF()
}
}
}.resume()
}

func displayPDF() {
let pdfview = PDFView(frame:self.view.bounds)
pdfview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
pdfview.autoScales = true
self.view.addSubview(pdfview)
let doc = PDFDocument(url: self.pdffileurl)
pdfview.document = doc
}

}

关于ios - 如何在 PDFKit 中显示从互联网下载的 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58522098/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com