TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: What will my PDF location be in local app files?

Forums > SwiftUI

I have sourced a function that converts a view to a PDF from another stack overflow post - https://stackoverflow.com/a/60753437/12969913. With this I am using another view to try and display the PDF inside a new view for the user to inevitably share and view as they wish.

How do I get my PDF to display, I can't figure out how the URL works and what I have to put into the new view.

The URL is created in the exportToPDF() function:

let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let outputFileURL = documentDirectory.appendingPathComponent("SwiftUI.pdf")

I have then sourced some code to display the PDF from another stack overflow post and have implemented that - https://stackoverflow.com/a/61480852/12969913, but in order for this to work I have to put in my file URL, how do I do that, and what will the file URL be?

My PDFKitView() Code - to display the new .pdf:

struct PDFKitView: View {
    var url: URL
    var body: some View {
        PDFKitRepresentedView(url)
    }
}

struct PDFKitRepresentedView: UIViewRepresentable {
    let url: URL
    init(_ url: URL) {
        self.url = url
    }

    func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
        let pdfView = PDFView()
        pdfView.document = PDFDocument(url: self.url)
        pdfView.autoScales = true
        return pdfView
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
        // Update the view.
    }
}

I then go to display this using:

HStack{
    PDFKitView(url: ????????)
    Text("Hello World")
}

3      

Hello,

well the URL will stay the same :-) Since you saved the file into the Documents Directory you can find it here.

let fileURL = documentDirectory.appendingPathComponent("SwiftUI.pdf")

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.