Creating a PDF in Swift with PDFKit

Learn how to create a PDF, work with Core Text and Core Graphics and share the created document by building an app that displays the user’s input on a flyer that can be shared with other iOS apps. By Bill Morefield.

4.9 (28) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Sharing the PDF

Open FlyerBuilderViewController.swift and replace the contents of shareAction() with:

// 1
guard 
  let title = flyerTextEntry.text, 
  let body = bodyTextView.text,
  let image = imagePreview.image, 
  let contact = contactTextView.text 
  else {
    // 2
    let alert = UIAlertController(
      title: "All Information Not Provided",
      message: "You must supply all information to create a flyer.",
      preferredStyle: .alert
    )
      alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
      present(alert, animated: true, completion: nil)
      return
  }
// 3
let pdfCreator = PDFCreator(
  title: title,
  body: body, 
  image: image, 
  contact: contact
) 
let pdfData = pdfCreator.createFlyer()
let vc = UIActivityViewController(
  activityItems: [pdfData],
  applicationActivities: []
) 
present(vc, animated: true, completion: nil)

Here you:

  1. First, ensure the user added all information for the flyer.
  2. If not, you create an informational message, display it to the user and return.
  3. If the user has added all the information for the flyer, you create the PDF. You then create a UIActivityViewController to provide and display the shared object's data.

Build and run the app, enter all information, then tap Share at the bottom-right side of the app. You'll see options to share the PDF file. Unfortunately, the simulator doesn't provide many useful options, so you may want to run the app on a real device.

your share sheet

One Final Touch

That check before sharing the PDF is a nice user interface feature. So now you'll add a check to make sure all information is there when the user requests a preview as well.

Open FlyerBuilderViewController.swift file and, above prepare(for:sender), add this new method to determine if the segue should occur:

override func shouldPerformSegue(withIdentifier identifier: String,
                                 sender: Any?) -> Bool {
  if 
    let _ = flyerTextEntry.text, 
    let _ = bodyTextView.text,
    let _ = imagePreview.image, 
    let _ = contactTextView.text { 
      return true 
    }

  let alert = UIAlertController(
    title: "All Information Not Provided",
    message: "You must supply all information to create a flyer.", 
    preferredStyle: .alert
  )
  alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
  present(alert, animated: true, completion: nil)

  return false
}

This code checks that the user has entered something in each text field and has selected an image. If so, it allows the segue. If not, it displays an error message and stops the segue.

Where to Go From Here?

You can download the final project using the Download Materials button at the top or bottom of this page.

In this tutorial, you've built an app that creates a PDF file using PDFKit. The user's input produces a flyer that you can share with other iOS apps. In the process, you learned to create a PDF, work with Core Text and Core Graphics and share the created document.

For more on PDFKit, look at Apple's PDFKit Documentation and the Introducing PDFKit on iOS — WWDC 2017 session.

For more on Core Text, see our Core Text Tutorial.

To learn more about Core Graphics, see Core Graphics Tutorial Part 1: Getting Started and Core Graphics Tutorial Part 2: Gradients and Contexts.

We hope you found this tutorial helpful. If you have any questions or comments, please join the forum discussion below.