Draw a PDF in an iOS App: UIGraphicsPDFRenderer and IronPDF

# ios# swift# pdf# uikit
Draw a PDF in an iOS App: UIGraphicsPDFRenderer and IronPDFIronSoftware

A travel app exports a trip expense report as a PDF, with a title, a handful of amounts and a notes...

Trip expense reports leave a travel app as PDFs, each with a title, a handful of amounts and a notes field that a traveler can fill with one sentence or with two pages about a canceled flight out of Lisbon. The short reports always look right. The long ones raise a question the drawing code has to answer itself, which is when page two begins.

We are preparing an iOS edition of IronPDF for iPhone and iPad apps, coming soon, with early-access sign-up. Meanwhile, the report is drawn with Apple's own UIGraphicsPDFRenderer.

We work at Iron Software, and the end of this article covers our library, IronPDF.

How does UIGraphicsPDFRenderer draw the first page?

It takes a page size and a closure of drawing instructions and returns the PDF. UIGraphicsPDFRenderer is the UIKit class for creating PDFs, with Swift and Objective-C interfaces, and its drawing closure runs against a Core Graphics context that the renderer creates.

// The renderer takes the page bounds, and the closure holds every drawing instruction.
let renderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 500, height: 300))
let pdf = renderer.pdfData { (context) in
  context.beginPage()
  let attributes = [
    NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 28)
  ]

  // The title is drawn into a rectangle whose position the code chooses.
  let title = "Expense report: Lisbon" as NSString
  title.draw(in: CGRect(x: 0, y: 0, width: 500, height: 200), withAttributes: attributes)
}
Enter fullscreen mode Exit fullscreen mode

Every position in that closure is set by the code, from the page bounds to the rectangle that holds the title, so the look of the report is a set of numbers in a source file.

Who decides where page two begins?

The app does, because the renderer only starts a page when the code asks for one. With UIGraphicsPDFRenderer, a page is created with beginPage() or beginPage(withBounds:pageInfo:) before any drawing instruction, and more pages are added by calling it again. The code that draws the notes is therefore also the code that decides when the notes need a new page.

let pdf = renderer.pdfData { (context) in
  let attributes = [
    NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 12)
  ]

  // The page count is fixed here, but real notes need measuring code to produce it.
  for page in 1...3 {
    context.beginPage()
    let text = "Notes, page \(page)" as NSString
    text.draw(in: CGRect(x: 0, y: 0, width: 500, height: 200), withAttributes: attributes)
  }
}
Enter fullscreen mode Exit fullscreen mode

The 3 is the hard part. Measuring code has to produce that number, together with the slice of text that belongs on each page.

How does the app measure long notes?

It hands the measuring to another framework. Short strings can be drawn with a method on NSString, and for longer text Apple suggests TextKit or Core Text, which provide the text layout.

The question has been asked in public. In February 2021, a developer building a PDF with Core Graphics asked on the Swift forums how the code should know when to call beginPage() and continue on a fresh page. One reply said the app will most likely have to track where page breaks go from the margin settings and the page size, using Core Text where necessary. Another described walking down the page, adding up paragraph heights and trimming words off the last paragraph until it fits. The thread shows no accepted answer.

Internal links add one more conversion. Named destinations and the link rectangles that jump to them use the underlying PDF coordinate space, whose y-axis is flipped against the one Core Graphics uses, and the code converts between them with the userSpaceToDeviceSpaceTransform property on CGContext.

What the renderer offers inside UIKit

As part of UIKit, the renderer needs no added dependency. It has been in UIKit since iOS 10.0 and is also available on iPadOS, Mac Catalyst, tvOS and visionOS. One renderer produces two outputs, pdfData(actions:) returning a Data object and writePDF(to:withActions:) saving the file to disk, and beginPage(withBounds:pageInfo:) lets a landscape receipt scan sit among portrait pages. Named destinations come from addDestination(withName:at:) and setDestinationWithName(_:for:), and external URLs from setURL(_:for:).

Could the expense report be an HTML template?

With IronPDF for iOS, that is the design. According to its product page, the edition will let iPhone and iPad apps create receipts, reports, tickets and signed forms from HTML, with CSS and JavaScript as part of the input, and edit PDFs they already hold, inside the app. The notes would become a paragraph in the template, laid out by the engine in place of measuring code.

On the device, the page says the engine has no network access of its own and that file bytes stay in the app's process, so the traveler's expenses would not leave the phone. Its feature list adds bookmarks, headers and footers, form filling with digital signatures, splitting and merging, password protection and conversion to PDF/A.

Which iOS documents go to the new edition first?

The documents with growing text go first, since that is where the page-start code lives.

  • Growing text first: the expense report with open-ended notes is the first test, rendered from an HTML template with the longest notes on file.
  • Designer-owned documents next: reports whose look changes more often than their data follow once the first report matches.
  • Linked documents after that: documents with receipt URLs move last, once their links have been checked in the new output.
  • While the edition is pending: keep the current renderer for production and join the early-access list for the iOS edition so the HTML template is ready for the first build.

The renderer details rest on Apple's UIKit documentation and the Swift forums thread on page breaks. Registration for early access is open on the IronPDF for iOS page on ironpdf.com, and the report with the longest notes is the one we would render first.

Drafted with AI. On November 26, 2026, the UIGraphicsPDFRenderer calls were checked against Apple's UIKit documentation, whose examples the two listings adapt with NSAttributedString.Key.font in place of NSFontAttributeName, the page-break question against the Swift forums thread, and the IronPDF for iOS claims against its product page.

UIKit, Apple, Swift, Objective-C, Core Graphics, Core Text, TextKit, iOS, iPadOS, iPhone, iPad, Mac Catalyst, tvOS and visionOS are trademarks of their respective owners, we have no affiliation with any of them, and the facts above come from Apple's UIKit documentation, the Swift forums thread on page breaks and the IronPDF for iOS page on ironpdf.com.