A quick PDF Viewer in SwiftUI

Hello Reader,

I have been working on a personal project which requires a PDF file to be loaded and editable. I chose Swift for this project and using Apple’s PDFKit package.

But then today, I just thought of trying out the basic PDF file loading and viewing via SwiftUI. I found SwiftUI to be really sleek, swift (aptly named), and fun to work on. Kind of felt it similar to Flutter framework, just a personal perspective.

Anyways, for reading a PDF document using PDFKit only two steps are required for Swift, and in case of SwiftUI, an additional step is needed :)

  1. Fetch a Document object from PDFDocument class by providing the URL/File path of your pdf file
  2. Assign this Document object to a PDFView class object
  3. You need to create a UIView generator wrapper for PDFView class, as SwiftUI doesn’t have a readily available UIView subclass for it.
Fetch a Document object from PDFDocument class
Create a UIView representable wrapper for PDFView class object

Now you can directly use this PDFViewUI object in SwiftUI framework layout

And this is how the app looks on iPad and iPhone sims

PS:

I also have a Swift only version of this app and with additional features like:

  • Add coloured annotations
  • Multi-page support
  • Undo feature