Pdfview Does Not Display My PDF

PDFView does not display my PDF

The problem is that the pdf you are importing it is just a temporary file. You need to copy/move the temporary file url to your app documents folder.

UIDocumentPickerModeImport The URL refers to a copy of the selected
document. This document is a temporary file. It remains available only
until your application terminates. To keep a permanent copy, you must
move this file to a permanent location inside your sandbox.

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
try FileManager.default.moveItem(at: url.standardizedFileURL, to: documentDirectory.appendingPathComponent(url.lastPathComponent))
tableView.reloadData()
} catch {
print(error)
}
}

PDF not shown correctly

Yeah, PDFView is annoying like that. set the PDFView's document to nil before assigning it a new document:

_pdfView.document = nil;
_pdfView.document = pdf;

This will reset it and solve your problem.

Also, you don't need to set needsDisplay on the window's view.

Cannot view PDF in PDFView IOS downloaded using Alamofire - SWIFT

PDFDocument(url: URL) only accepts a file URL, but in your code you say:

let fileURL = URL.init(string: self.containerPDFUrl)
let pdfDoc = PDFDocument.init(url: fileURL!)

You say that self.containerPDFUrl is of the type

https://example.org/rab1Mpub.pdf

But that is not a file url. I believe you intended:

let pdfDoc = PDFDocument(url: self.destination.0)

But let fileURL = documentsURL.appendingPathComponent("pig.png") should likely be let fileURL = documentsURL.appendingPathComponent("pig.pdf")

Update

if response.error == nil, let pdfFilePath = response.destinationURL?.path {

print("inside DispatchQueue")
print("pdfFilePath: \(pdfFilePath)")

DispatchQueue.main.async{
// Instantiate PDFDocument
let docURL = URL.init(string: pdfFilePath)
let pdfDoc = PDFDocument.init(url: docURL!)
self.myPDFView.document = pdfDoc

}
}

There's no need to deal with paths here. Simply take the destinationURL and init the document from that. For example:

if response.error == nil, let pdfURL = response.destinationURL? {

DispatchQueue.main.async{
// Instantiate PDFDocument
let pdfDoc = PDFDocument(url: pdfURL)
self.myPDFView.document = pdfDoc

}
}


Related Topics



Leave a reply



Submit