Skip to content

Instantly share code, notes, and snippets.

@siliconsorcery
Last active August 13, 2022 02:00
Show Gist options
  • Save siliconsorcery/aacf39e820e7222e070c60c89cd7aa5e to your computer and use it in GitHub Desktop.
Save siliconsorcery/aacf39e820e7222e070c60c89cd7aa5e to your computer and use it in GitHub Desktop.
PDF View in SwiftUI
import SwiftUI
import PDFKit
struct PDFPage: View {
var body: some View {
let url = URL(string: "https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture4.pdf")!
let pdfDoc = PDFDocument(url: url)!
ZStack {
Color.yellow
PDFKitView(showing: pdfDoc)
}
.ignoresSafeArea()
}
struct PDFKitView: UIViewRepresentable {
let pdfDocument: PDFDocument
init(showing pdfDoc: PDFDocument) {
self.pdfDocument = pdfDoc
}
//you could also have inits that take a URL or Data
func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.document = pdfDocument
pdfView.autoScales = true
return pdfView
}
func updateUIView(_ pdfView: PDFView, context: Context) {
pdfView.document = pdfDocument
}
}
}
@vinniedaboi
Copy link

How would you get user input example the link and process it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment