# Creating a PDF in Swift with PDFKit | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105>\
**Category:** 🎓 Courses & Articles\
**Created:** [August 27, 2019, 2:22am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105 "2019-08-27T02:22:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [August 27, 2019, 2:22am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/1 "2019-08-27T02:22:42Z")

</div>

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.

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit](https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit)

---

<div class="post-metadata">

**Author:** ![fraggedressources](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fraggedressources/32/81494_2.png) [@fraggedressources](https://forums.kodeco.com/u/fraggedressources)\
**Post date:** [October 13, 2019, 7:35pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/2 "2019-10-13T19:35:21Z")

</div>

How to add a second page?

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [October 13, 2019, 10:30pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/3 "2019-10-13T22:30:02Z")

</div>

You call `context.beginPage()` again to start a new page.

---

<div class="post-metadata">

**Author:** ![bbongrip](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bbongrip/32/102075_2.png) [@bbongrip](https://forums.kodeco.com/u/bbongrip)\
**Post date:** [November 2, 2019, 7:19pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/4 "2019-11-02T19:19:08Z")

</div>

Great tutorial! One question, how would I add a paragraph with a dynamic height in relation to the pages width, not the attributed text size() width? The size() function only returns the size with a crazy long width. Thanks in advance

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [November 12, 2019, 3:39am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/5 "2019-11-12T03:39:26Z")

</div>

Look at the `.size(withAttributes:)` on String. I think it will do what you’re looking for.

You also may want to take a look at the tutorial at [https://www.raywenderlich.com/578-core-text-tutorial-for-ios-making-a-magazine-app](https://www.raywenderlich.com/578-core-text-tutorial-for-ios-making-a-magazine-app). It goes into CoreText formatting and layout in more depth.

---

<div class="post-metadata">

**Author:** ![camrawne](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/camrawne/32/86555_2.png) [@camrawne](https://forums.kodeco.com/u/camrawne)\
**Post date:** [December 20, 2019, 7:57pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/6 "2019-12-20T19:57:02Z")

</div>

Is there a simple way to add a hyperlink?

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [December 20, 2019, 11:49pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/7 "2019-12-20T23:49:19Z")

</div>

@camrawne You should be able to use an `NSAttributedString` and set the `NSAttributedString.Key.link` attribute on it.

---

<div class="post-metadata">

**Author:** ![vegepilot](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vegepilot/32/102075_2.png) [@vegepilot](https://forums.kodeco.com/u/vegepilot)\
**Post date:** [February 25, 2020, 6:49pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/8 "2020-02-25T18:49:43Z")

</div>

Really appreciate this tutorial! I’m using paintCode to build graphics for a complex pdf form. Naturally everything is upside down because of the different coordinate systems. You mentioned that PDFKit has some tools to help with the conversion. Can you point me in the right direction for those?

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [February 26, 2020, 12:40am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/9 "2020-02-26T00:40:10Z")

</div>

@vegepilot I’m happy to hear you found the tutorial helpful. I think what you’re looking for is `userSpaceToDeviceSpaceTransform`. You can find more information about it in the Apple documentation for `UIGraphicsPDFRenderer` at [Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uigraphicspdfrenderer)

---

<div class="post-metadata">

**Author:** ![jel111](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jel111/32/89357_2.png) [@jel111](https://forums.kodeco.com/u/jel111)\
**Post date:** [March 29, 2020, 12:15am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/10 "2020-03-29T00:15:04Z")

</div>

In the area of adding images, this is what it says to do.

Next, you need to pass the image to PDFCreator. Open FlyerBuilderViewController.swift and replace the implementation of `prepare(for:sender:)` with:

```
if 
  let title = flyerTextEntry.text, 
  let body = bodyTextView.text,
  let image = imagePreview.image {
    let pdfCreator = PDFCreator(title: title, body: body,
                                image: image, contact: "")
    vc.documentData = pdfCreator.createFlyer()
}

```

Here is what I have before replacing with above which compiles.

```
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "previewSegue" {
      guard
        segue.identifier == "previewSegue",
        let vc = segue.destination as? PDFPreviewViewController,
        let title = flyerTextEntry.text,
        let body = bodyTextView.text

        else {
          return
      }
      
      let pdfCreator = PDFCreator(
        title: title,
        body: "",
        image: UIImage(),
        contact: ""
      )
      vc.documentData = pdfCreator.createFlyer()
    }
  }

```

To replace it and make it work you need to leave in the line

`let vc = segue.destination as? PDFPreviewViewController,`

Which it then should look like this

```
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if
      let vc = segue.destination as? PDFPreviewViewController,
      let title = flyerTextEntry.text,
      let body = bodyTextView.text,
      let image = imagePreview.image {
        let pdfCreator = PDFCreator(title: title, body: body,
                                    image: image, contact: "")
    vc.documentData = pdfCreator.createFlyer()
    }

  }

```

And just a note, the Final file in the downloaded materials looks nothing like the actual completed app.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 6, 2020, 10:45am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/11 "2020-04-06T10:45:03Z")

</div>

@bmorefield Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [April 8, 2020, 11:01pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/12 "2020-04-08T23:01:23Z")

</div>

@jel111 I think you’re right the wording is a bit off there. As you noted it should be to replace part of the guard statement and not the entire method.

---

<div class="post-metadata">

**Author:** ![rpemberton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rpemberton/32/102075_2.png) [@rpemberton](https://forums.kodeco.com/u/rpemberton)\
**Post date:** [April 24, 2020, 5:08pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/13 "2020-04-24T17:08:17Z")

</div>

I get a Build time error running this that Cycle inside FlyerBuilder; building could produce unreliable results Cycle details …

What does this mean?

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [April 26, 2020, 4:29pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/14 "2020-04-26T16:29:58Z")

</div>

Hmm, those errors usually relate to the build process. I checked against the latest Xcode and don’t see the error. Can you provide a bit more detail or any changes you might have made to the project?

---

<div class="post-metadata">

**Author:** ![rpemberton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rpemberton/32/102075_2.png) [@rpemberton](https://forums.kodeco.com/u/rpemberton)\
**Post date:** [April 26, 2020, 5:28pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/15 "2020-04-26T17:28:02Z")

</div>

I made no changes to the code just tried to compile and run it.  
I did find a article that said this was related to a build setting issue which seemed to allow it to work. Can’t remember what it was off top of my head. But as soon as I’m back at desk I’ll look it up.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 5, 2020, 1:17am UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/16 "2020-07-05T01:17:17Z")

</div>

@rpemberton Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![habiba](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/habiba/32/81494_2.png) [@habiba](https://forums.kodeco.com/u/habiba)\
**Post date:** [July 9, 2020, 11:01pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/17 "2020-07-09T23:01:15Z")

</div>

Is it applicable to MacOS? Any alternative to UIGraphicsPDFRendererFormat() to use in macOS. macOS cannot identify it. Thanks in advance for the great tutorial

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [July 13, 2020, 10:48pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/18 "2020-07-13T22:48:10Z")

</div>

It’s focused on UIKit and would work using Catalyst. For the macOS, look at `PDFDocument`.

---

<div class="post-metadata">

**Author:** ![heide](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/heide/32/81494_2.png) [@heide](https://forums.kodeco.com/u/heide)\
**Post date:** [October 15, 2020, 1:31pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/19 "2020-10-15T13:31:26Z")

</div>

Hi,

I really love this tutorial.  
I am wondering how you would create a multipage PDF using an array of (for example) images. Where each image will be on it’s own page. Are you please able to help out?

---

<div class="post-metadata">

**Author:** ![bmorefield](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bmorefield/32/111506_2.png) [@bmorefield](https://forums.kodeco.com/u/bmorefield)\
**Post date:** [October 18, 2020, 5:13pm UTC](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105/20 "2020-10-18T17:13:53Z")

</div>

@heide You’d need to loop through the images, create a new page for each, then use the code from the tutorial on drawing an image to a page.

[Next page](https://forums.kodeco.com/t/creating-a-pdf-in-swift-with-pdfkit-raywenderlich-com/80105.md?page=2)
