In this article we describe how to add a PDF viewer to your iOS app using Swift with PDFKit, Apple's builtin PDF SDK introduced in iOS 11.

The PDFKit framework has been available in AppKit since macOS 10.4 but was a private framework on iOS used in the iBooks and Mail apps. However, the Core Graphics framework for iOS provides basic PDF handling without any UI. Using the Core Graphics PDF API directly to create a PDF viewer requires a lot of work, so the introduction of PDFKit for iOS saves a large amount of time.

linkStep 1 - Import the module

In your Swift iOS project where you want to display a PDF, usually in a UIViewController subclass, import the PDFKit module at the top of the Swift file:

import PDFKit

This will make the PDFKit framework available for use in the Swift file.

linkStep 2 - Implement the viewer

To display a PDF document, a PDFView is created and added to the view controller's view hierarchy. In the following sample, the UIViewController.viewDidLoad() method is used to set up the PDFView and load a PDF document from the app bundle:

override func viewDidLoad() {

    // ...

    // Add PDFView to view controller.
    let pdfView = PDFView(frame: self.view.bounds)
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    // Fit content in PDFView.
    pdfView.autoScales = true
    // Load Sample.pdf file from app bundle.
    let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")
    pdfView.document = PDFDocument(url: fileURL!)

In the sample above, the PDFView is set to fill the entire view controller and automatically scale its content to fit the available space. The "Sample.pdf" file is loaded from the app's bundled resources, but it is also possible to use other URL sources or a Data object.


PDFKit viewer.

As you can see from the preview, the PDF document is displayed but there are no controls to add annotations, search for text, etc.

Apple's PDFKit framework is an easy way to add a basic PDF viewer, but if you need more robust functionality, like annotation creation and editing, real-time collaboration, form filling, text search, page manipulation, or others, you would have to implement those features yourself. Also beware that PDFKit's rendering engine only works well if your documents are simple and you can afford to have some rendering inconsistencies or failures.

For more robust functionality and reliable rendering, PDFTron for iOS is available. Some of the features included are:

  • Annotation creation and editing
  • Form filling
  • Text search
  • Page manipulation
  • Customizable UI controls
  • Multi-tabbed PDF viewing
  • Reflow
  • Night and custom viewing modes
  • And many more

PDFTron for iOS not only supports PDF documents but also other formats including .docx, .doc, .pptx, .xlsx, Markdown, and various image formats.

linkAdding a PDF viewer with PDFTron

To show a PDF with PDFTron, download and integrate the PDFTron SDK for iOS into your project.

The PDFTron PTDocumentViewController is a full-featured PDF viewer that can be added to your app with a few lines of code. In the following sample, a PTDocumentViewController is shown from a UIViewController subclass's viewDidAppear(_:) method:

override func viewDidAppear(_ animated: Bool) {
    // ...
    // Create PTDocumentViewController.
    let documentController = PTDocumentViewController()
    // Load Sample.pdf file from app bundle.
    let fileURL = Bundle.main.url(forResource: "Sample", withExtension: "pdf")
    do {
        try documentController.openDocument(with: fileURL!)
    } catch {
        print("Failed to open document: \(error)")
    // Show PTDocumentViewController in UINavigationController.
    let navigationController = UINavigationController(rootViewController: documentController)
    self.present(navigationController, animated: true, completion: nil)

In the sample above, a PTDocumentViewController is created and added to a UINavigationController that supplies the navigation bar. The sample document is loaded from the app bundle, but the PTDocumentViewController class can also open files from other locations.


PDFTron viewer.

You can find the API documentation for the PTDocumentViewController class here.


Using Apple's PDFKit framework to add a PDF viewer to your app is simple, but if you want more advanced features and need rock-solid rendering and support, then PDFKit's simplicity becomes a liability. PDFTron has hundreds of features, supports 30+ file formats, has a proven builtin rendering engine, and is just as simple to integrate.

You can view and download the full source code for this article here.

If you have any questions about integrating PDFTron into your project, please feel free to contact us and we'll be more than happy to help!