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")
}