生成PDF,但在swift ios的webView中未检索到文件
Posted
技术标签:
【中文标题】生成PDF,但在swift ios的webView中未检索到文件【英文标题】:PDF is generated but file not retrieved in a webView in swift ios 【发布时间】:2018-01-22 09:56:34 【问题描述】:我正在生成 UIView
的 PDF 并在 myforms
中存储名称,正在生成 PFD 并且控制台打印 PDF 可用,但它在 VC 中只显示白屏
这就是我正在做的事情:
我有这个代码来生成 PDF
@IBAction func createAct(_ sender: Any)
print("Creat PDF")
self.createPdfFromView(mainView: mainView, saveToDocumentsWithFileName: "myforms")
func createPdfFromView(mainView: UIView, saveToDocumentsWithFileName fileName: String)
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, mainView.bounds, nil)
UIGraphicsBeginPDFPage()
guard let pdfContext = UIGraphicsGetCurrentContext() else return
mainView.layer.render(in: pdfContext)
UIGraphicsEndPDFContext()
if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let documentsFileName = documentDirectories + "/" + fileName
debugPrint(documentsFileName)
pdfData.write(toFile: documentsFileName, atomically: true)
然后我在控制台中有这个:
创建 PDF
"/Users/jawaidahmed/Library/Developer/CoreSimulator/Devices/3F95BE09-0F25-427C-9ED9-4BDB16DC3600/data/Containers/Data/Application/1EC22FBC-79E7-4AC5-BB7D-13783CC34516/Documents/myforms"
我正在使用这个PDFViewController
在 web 视图中检索文件
override func viewDidLoad()
super.viewDidLoad()
let check = "myforms"
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
print("check final = \(pdfURL)")
pdfURL = pdfURL.appendingPathComponent(check) as URL
if let pdfBundleURL = Bundle.main.url(forResource: "myforms", withExtension: nil, subdirectory: nil)
do
let data = try Data(contentsOf: pdfBundleURL)
//Lastly, write your file to the disk.
try data.write(to: pdfURL, options: .atomicWrite)
catch
// catch errors here
do
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
var filePath = url.appendingPathComponent(check).path
let fileManager1 = FileManager.default
if fileManager1.fileExists(atPath: filePath)
print("FILE AVAILABLE in VC")
filePath = "file://\(filePath)"
let fileUrlkk = Foundation.URL(string: filePath)
let data = try Data(contentsOf: fileUrlkk!)
self.webView.load(data, mimeType: filePath, textEncodingName:"utf-8", baseURL: pdfURL.deletingLastPathComponent())
else
print("FILE NOT AVAILABLE in VC")
catch let error as NSError
print("An error took place: \(error)")
我在控制台中收到了这个:
FILE AVAILABLE in VC
这意味着文件已生成并且可用,但它只显示空的VC白色背景
【问题讨论】:
执行后filePath
是什么:var filePath = url.appendingPathComponent(check).path
?
当您保存文档时,您没有编写扩展名“.pdf”。
@Alexkater 那我该怎么做
@iosDeveloper 在这一行 self.createPdfFromView(mainView: mainView, saveToDocumentsWithFileName: "myforms.pdf")
@Alexkater 它不工作亲爱的!
【参考方案1】:
self.webView.load(data, mimeType: "application/pdf", textEncodingName:"utf-8", baseURL: pdfURL.deletingLastPathComponent())
替换 textEncodingName:""
与
textEncodingName:"utf-8"
检查一下
【讨论】:
【参考方案2】:尝试在 Webview 中加载。我尝试使用您的示例。它有效
let FileName = "myforms"
let Filemanager = NSFileManager.defaultManager()
let docURL = Filemanager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
print(docURL)
let pdfPATH = docURL.URLByAppendingPathComponent(FileName)
print(pdfPATH)
let data = NSData(contentsOfURL: pdfPATH)
WBview.loadData(data!, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: pdfPATH.URLByDeletingLastPathComponent!)
【讨论】:
以上是关于生成PDF,但在swift ios的webView中未检索到文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Swift 中从 NSData 显示 PDF - 如何将 PDF 保存到文档文件夹 Swift - 如何在 Swift 中通过 WebView 从保存的 NSData 显示 PDF
在 Swift 中通过 WebView 下载 .pdf 或 .ppt
使用 Swift 3 触摸时在 Webview 中获取 pdf 的实际坐标