I have successfully download pdf file from Internet and it is saved in documents directory.
The url is as follows of the downloaded file
file:///Users/heetshah/Library/Developer/CoreSimulator/Devices/4BF83AAF-A910-46EB-AE76-91BC6BEED033/data/Containers/Data/Application/B4532805-2842-431F-B16C-C5E448C8366F/Documents/TPAFForm.pdf
I am trying to display it to PDFKit as follows.
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
}
I am not getting any error
I have went through bunch of stack overflow posts and it is displaying the same solution as above but it does not work in my case.
I also tried following solution but it does not work
if let path = Bundle.main.path(forResource: pdfUrl, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {..
Following is my code to download the file
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
}
}
}
}
I am using Alamofire to download the file. There constraint for my PDFView are proper as I am able to display an online url pdf in my preview but I need to download the pdf locally first and then display it in my pdf view
Since you have not shown sufficient code to debug the problem, here is complete code for doing what you describe, and you can debug your problem by comparing your code to mine:
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
}
}
Related
I have downloaded a pdf file from web service. I have found it in device container.My file location is bellow
file:///var/mobile/Containers/Data/Application/1E4AFEDC-E3A6-4E33-9021-217A61567597/Documents/myfile.pdf
But when i want to open the file from above location then nothing happening. Here is my code..
let pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
guard let path = Bundle.main.url(forResource: "file:///var/mobile/Containers/Data/Application/8180A938-D770-48AE-9FC7-ADE939B1D9FA/Documents/myfile", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
Please help & suggest me..
The issue is that you are trying to access a file that's clearly not part of the application Bundle, since the Bundle is read-only, so a file downloaded from the internet is not stored in the bundle. Moreover, you supply a full filepath to Bundle.main.url(forResource:), whereas that function only expects a local path to bundled files.
You need to use URL(fileURLWithPath:) instead of Bundle.url(forResource:).
let pdfUrl = URL(fileURLWithPath: "file:///var/mobile/Containers/Data/Application/8180A938-D770-48AE-9FC7-ADE939B1D9FA/Documents/myfile.pdf")
if let document = PDFDocument(url: path) {
pdfView.document = document
}
You need to get the Document path with this :
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectory: NSURL = urls.first as? NSURL {
// This is where the database should be in the documents directory
let finalFileURL = documentDirectory.URLByAppendingPathComponent("myfile.pdf")
if finalFileURL.checkResourceIsReachableAndReturnError(nil) {
// The file already exists, so just return the URL
return finalFileURL
}
} else {
println("Couldn't get documents directory!")
}
In Swift 5 working this code with PDFKit
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent("myfile.pdf") as URL
pdfView.document = PDFDocument(url: pdfURL )
I am using ARKit for my application and I try to dynamically load .scn files from my web-server(URL)
Here is a part of my code
let urlString = "https://da5645f1.ngrok.io/mug.scn"
let url = URL.init(string: urlString)
let request = URLRequest(url: url!)
let session = URLSession.shared
let downloadTask = session.downloadTask(with: request,
completionHandler: { (location:URL?, response:URLResponse?, error:Error?)
-> Void in
print("location:\(String(describing: location))")
let locationPath = location!.path
let documents:String = NSHomeDirectory() + "/Documents/mug.scn"
ls = NSHomeDirectory() + "/Documents"
let fileManager = FileManager.default
if (fileManager.fileExists(atPath: documents)){
try! fileManager.removeItem(atPath: documents)
}
try! fileManager.moveItem(atPath: locationPath, toPath: documents)
print("new location:\(documents)")
let node = SCNNode()
let scene = SCNScene(named:"mug.scn", inDirectory: ls)
let nodess = scene?.rootNode.childNode(withName: "Mug", recursively: true)
node.addChildNode(nodess!)
let nodeArray = scene!.rootNode.childNodes
for childNode in nodeArray {
node.addChildNode(childNode as SCNNode)
}
self.addChildNode(node)
self.modelLoaded = true
})
downloadTask.resume()
Nslog:
location:Optional(file:///private/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/tmp/CFNetworkDownload_duDlwf.tmp)
new location:/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/Documents/mug.scn
.scn file downloading with the above mentioned(new location) file path.. but when i try to use this downloaded file in SCNScene
let scene = SCNScene(named:"mug.scn", inDirectory: ls)
always scene value is nil.
error
Thread 4: Fatal error: Unexpectedly found nil while unwrapping an Optional value
how to resolve this issues. Thank you
About init?(named: String), the documentation says:
Loads a scene from a file with the specified name in the app’s main bundle
since you don't have such file inside the main bundle (is coming from a download), you may try with the following constructor:
init(url: URL, options: [SCNSceneSource.LoadingOption : Any]? = nil)
so your code might be:
do {
let documents = "yourValidPath"
let scene = try SCNScene(url: URL(fileURLWithPath: documents), options: nil)
} catch {}
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let pathToObject = documentDirectory + "ship.scn"
let fileUrl = URL(fileURLWithPath: pathToObject)
guard let cshipScene = try? SCNScene(url: fileUrl, options: nil) else { return }
let shipNode = SCNNode()
let shipSceneChildNodes = shipScene.rootNode.childNodes
for childNode in shipSceneChildNodes {
shipNode.addChildNode(childNode)
}
node.addChildNode(shipNode)
I am generating a PDF of a UIView and storing with a name in myforms the PFD is being generated and console prints PDF is Available but it shows nothing but a white screen in VC
here is what I am doing :
I have this code to generate a 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)
}
}
then I have this in console :
Creat PDF
"/Users/jawaidahmed/Library/Developer/CoreSimulator/Devices/3F95BE09-0F25-427C-9ED9-4BDB16DC3600/data/Containers/Data/Application/1EC22FBC-79E7-4AC5-BB7D-13783CC34516/Documents/myforms"
and I am using this PDFViewController to retrieve the file in a webview
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)")
}
}
And I am receiving this in console :
FILE AVAILABLE in VC
It means file is generated and available but it shows only empty VC white background only
self.webView.load(data, mimeType: "application/pdf", textEncodingName:"utf-8", baseURL: pdfURL.deletingLastPathComponent())
replace textEncodingName:""
with
textEncodingName:"utf-8"
check it
Try this for loading in Webview.I tried with your sample..It works
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!)
I have downloaded a pdf from remote server and saved it in document directory. Now I'm trying to retrieve it and display the pdf in the webView but I keep getting this exception:
failed to find PDF header: `%PDF' not found.
before this exception it showing this exception as well:
objc[8087]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x11f29dcc0) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x11f0b46f0).
One of the two will be used. Which one is undefined.
But I have noticed that when I try to run from mobile instead of emulator then this second exception is gone. Below is my code for fetching it:
let check:String = FileNames[0] + ".pdf"
print("check = \(check)")
// Method 1
let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let targetURL = docURL.appendingPathComponent(check)
var request = URLRequest(url: targetURL)
webView.loadRequest(request)
/*
// Method 2
var pdfURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as URL
pdfURL = pdfURL.appendingPathComponent(check) as URL
print("check url = \(pdfURL)")
let data = try! Data(contentsOf: pdfURL)
print("check data = \(data)")
webView.load(data, mimeType: "application/pdf", textEncodingName:"utf-8", baseURL: pdfURL) // pdfURL.deletingLastPathComponent()
*/
//let requestk = NSURLRequest(url: pdfURL as URL)
// webView.loadRequest(requestk as URLRequest)
// Method 3
/* let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
var pdf = documentsUrl.appendingPathComponent(check)
print("check item fetching from documentsUrl = \(pdf)")
let req = NSURLRequest(url: pdf!)
self.webView.loadRequest(req as URLRequest)
*/
I have searched a lot about this exception and followed all the possible solutions but none of it is working. But if I try to display this pdf directly from the remote server's address it is getting displayed. And also I have checked that this pdf is stored correctly. I have tried loading through both webView.loadRequest and webView.load with data method, maybe I'm missing something small.
Update
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
print("check final = \(pdfURL)")
pdfURL = pdfURL.appendingPathComponent(check) as URL
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")
// let fileUrlkk = NSURL(string: filePath)// converting string into URL
filePath = "file://\(filePath)"
let fileUrlkk = Foundation.URL(string: filePath)
let data = try Data(contentsOf: fileUrlkk!)
// let data = try Data(contentsOf: pdfURL) // tried but didn’t work
self.webView.load(data, mimeType: "application/pdf", textEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
} else {
print("FILE NOT AVAILABLE in VC")
}
}
catch let error as NSError {
print("An error took place: \(error)")
}
it displays "FILE AVAILABLE in VC" but still with this exception.
Use below code to load PDF in WebView
Swift 3.0
var pdfURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent("Swift.pdf") as URL
let data = try! Data(contentsOf: pdfURL)
self.webView.load(data, mimeType: "application/pdf", textEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
Where pdf file store in Document directory here is the path.
/Documents/Swift.pdf
----- UPDATE ------
Create new project.
Drag 1 sample PDF in bundle "sample.pdf"
And change your controller code with below
that's it run
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let check = "Swift.pdf"
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: "sample", withExtension: "pdf", subdirectory: nil, localization: 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")
// let fileUrlkk = NSURL(string: filePath)// converting string into URL
filePath = "file://\(filePath)"
let fileUrlkk = Foundation.URL(string: filePath)
let data = try Data(contentsOf: fileUrlkk!)
// let data = try Data(contentsOf: pdfURL) // tried but didn’t work
self.webView.load(data, mimeType: "application/pdf", textEncodingName:"", baseURL: pdfURL.deletingLastPathComponent())
} else {
print("FILE NOT AVAILABLE in VC")
}
}
catch let error as NSError {
print("An error took place: \(error)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
just use
do{
let directoryURL = try manager.url(for:.documentDirectory,in:.userDomainMask, appropriateFor:nil, create:true)
let docURL = NSURL(string:"XXX.pdf", relativeTo:directoryURL)
}
catch{print("ERROR")
}
Webkit loading from .documentsURL html webpages, iOS device is not showing image, js, css files, works fine on simulators. What could be my bug in the code I have written?
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let documentsURL = URL(fileURLWithPath: documentsPath, isDirectory: true)
let urltostr = documentsURL.absoluteString
//print(urltostr)
let htmlurl = urltostr+"0/index.html"
let finalURL = URL(string: htmlurl)
let base = urltostr+"0/"
let baseUrl = URL(string: base)
//let baseUrl = URL(fileURLWithPath: base, isDirectory: true)
do {
let fileName = try String(contentsOf: finalURL!)
webview.loadHTMLString(fileName as String, baseURL: baseUrl)
} catch {
// catch error
print("Error web view get html")
}