---
title: UIGraphicsPDFRenderer
framework: uikit
role: symbol
role_heading: Class
path: uikit/uigraphicspdfrenderer
---

# UIGraphicsPDFRenderer

A graphics renderer for creating PDFs.

## Declaration

```swift
class UIGraphicsPDFRenderer
```

## Overview

Overview You can use PDF renderers to create PDF files, without having to manage Core Graphics contexts. To render a PDF: Optionally create a UIGraphicsPDFRendererFormat object to specify nondefault parameters the renderer should use to create its context. Instantiate a UIGraphicsPDFRenderer object, providing the dimensions of the output image and a format object. The renderer uses sensible defaults for the current device if you don’t provide format object, as demonstrated in Creating a graphics PDF renderer. Choose one of the rendering methods depending on your desired output: pdfData(actions:) outputs the PDF in the form of a Data object, and writePDF(to:withActions:) saves the PDF as a file directly to disk. Provide Core Graphics drawing instructions within the closure associated with your chosen method, as shown in Creating a PDF with a PDF renderer. Optionally, you can create a multi-page PDF, using the approach shown in Adding pages. Optionally, add links to your PDF to make navigation easy, as shown in Creating internal links. After initializing a PDF renderer, you can use it to draw multiple PDFs with the same configuration. Creating a graphics PDF renderer Create a PDF renderer, providing the bounds of the PDF page. You can instead use one of the other UIGraphicsPDFRenderer initializers to specify a renderer format (UIGraphicsPDFRendererFormat) in addition to the bounds. This allows you to configure the underlying Core Graphics context with custom PDF document info. If you don’t provide a format, the renderer uses the default() format, which creates a context best suited for the current device. Creating a PDF with a PDF renderer Use the pdfData(actions:) method to create a PDF with the PDF renderer you created above. This takes a block that represents the drawing actions. Within this block, the renderer creates a Core Graphics context using the parameters provided during renderer initialization, and sets this to be the current context. Before issuing PDF drawing instructions, you must create a page with a call to either the beginPage() method or beginPage(withBounds:pageInfo:) method on the supplied UIGraphicsPDFRendererContext. The drawing actions closure takes a single argument of type UIGraphicsPDFRendererContext. This provides access to some high-level drawing functions, such as fill(_:) through the UIGraphicsRendererContext superclass. note: This code uses a drawing method on NSString. If you want to create a PDF with more text, consider using TextKit or Core Text, both of which provide extensive text layout functionality. The above code creates the following result:

Adding pages Add multiple pages to your PDF through repeated calls to the beginPage() method on the UIGraphicsPDFRendererContext provided to the drawing block. Use the beginPage(withBounds:pageInfo:) method instead of the beginPage() method if you want to override the default properties for the new page. This code creates a PDF with three pages, each of which contains the current page number as large text, as shown in the following image.

Creating internal links You can create internal links, known as destinations, in PDFs. A complete link has two components: A named destination. This is a point on a PDF page. You create these with the addDestination(withName:at:) method on UIGraphicsPDFRendererContext. A link region. This is a rectangle on a PDF page, which when tapped, instructs the PDF viewing app to jump to a specific named destination. You create these with the setDestinationWithName(_:for:) method on UIGraphicsPDFRendererContext, providing the name of the destination to jump to, and the bounds of the active link region. The following code demonstrates how to use destinations with a PDF renderer by showing how to create links that jump to the next page. This code adds large red labels that jump from the current page to the next page when clicked. Each page has a destination with names of the form page-1, positioned at the origin. The bounding box for the next-page label is the link to the destination on the following page. note: The addDestination(withName:at:) and setDestinationWithName(_:for:) methods on UIGraphicsPDFRendererContext use the underlying PDF coordinate space, which has its y-axis flipped with respect to the coordinate system used by Core Graphics. You can translate between the two using the userSpaceToDeviceSpaceTransform property on CGContext, as shown in the code. The above code results in the following PDF:

## Topics

### Creating a PDF renderer

- [init(bounds:format:)](uikit/uigraphicspdfrenderer/init(bounds:format:).md)

### Managing the PDF data

- [pdfData(actions:)](uikit/uigraphicspdfrenderer/pdfdata(actions:).md)
- [writePDF(to:withActions:)](uikit/uigraphicspdfrenderer/writepdf(to:withactions:).md)
- [UIGraphicsPDFRenderer.DrawingActions](uikit/uigraphicspdfrenderer/drawingactions.md)

## Relationships

### Inherits From

- [UIGraphicsRenderer](uikit/uigraphicsrenderer.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Graphics contexts

- [UIGraphicsRenderer](uikit/uigraphicsrenderer.md)
- [UIGraphicsRendererContext](uikit/uigraphicsrenderercontext.md)
- [UIGraphicsRendererFormat](uikit/uigraphicsrendererformat.md)
- [UIGraphicsImageRenderer](uikit/uigraphicsimagerenderer.md)
- [UIGraphicsImageRendererContext](uikit/uigraphicsimagerenderercontext.md)
- [UIGraphicsImageRendererFormat](uikit/uigraphicsimagerendererformat.md)
- [UIGraphicsPDFRendererContext](uikit/uigraphicspdfrenderercontext.md)
- [UIGraphicsPDFRendererFormat](uikit/uigraphicspdfrendererformat.md)
