<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIGraphicsPDFRenderer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UIGraphicsPDFRenderer"
  },
  "title" : "UIGraphicsPDFRenderer"
}
-->

# UIGraphicsPDFRenderer

A graphics renderer for creating PDFs.

```
class UIGraphicsPDFRenderer
```

## Overview

You can use PDF renderers to create PDF files, without having to manage Core Graphics contexts.

To render a PDF:

1. Optionally create a [`UIGraphicsPDFRendererFormat`](/documentation/UIKit/UIGraphicsPDFRendererFormat) object to specify nondefault parameters the renderer should use to create its context.
2. Instantiate a [`UIGraphicsPDFRenderer`](/documentation/UIKit/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`](/documentation/UIKit/UIGraphicsPDFRenderer#Creating-a-graphics-PDF-renderer).
3. Choose one of the rendering methods depending on your desired output: [`pdfData(actions:)`](/documentation/UIKit/UIGraphicsPDFRenderer/pdfData(actions:)) outputs the PDF in the form of a <doc://com.apple.documentation/documentation/Foundation/Data> object, and [`writePDF(to:withActions:)`](/documentation/UIKit/UIGraphicsPDFRenderer/writePDF(to:withActions:)) saves the PDF as a file directly to disk.
4. Provide Core Graphics drawing instructions within the closure associated with your chosen method, as shown in [`Creating a PDF with a PDF renderer`](/documentation/UIKit/UIGraphicsPDFRenderer#Creating-a-PDF-with-a-PDF-renderer).
5. Optionally, you can create a multi-page PDF, using the approach shown in [`Adding pages`](/documentation/UIKit/UIGraphicsPDFRenderer#Adding-pages).
6. Optionally, add links to your PDF to make navigation easy, as shown in [`Creating internal links`](/documentation/UIKit/UIGraphicsPDFRenderer#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.

```objc
UIGraphicsPDFRenderer *renderer = [[UIGraphicsPDFRenderer alloc] initWithBounds:CGRectMake(0, 0, 500, 300)];
```

You can instead use one of the other [`UIGraphicsPDFRenderer`](/documentation/UIKit/UIGraphicsPDFRenderer) initializers to specify a renderer format ([`UIGraphicsPDFRendererFormat`](/documentation/UIKit/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()`](/documentation/UIKit/UIGraphicsRendererFormat/default()) format, which creates a context best suited for the current device.

### Creating a PDF with a PDF renderer

Use the [`pdfData(actions:)`](/documentation/UIKit/UIGraphicsPDFRenderer/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()`](/documentation/UIKit/UIGraphicsPDFRendererContext/beginPage()) method or [`beginPage(withBounds:pageInfo:)`](/documentation/UIKit/UIGraphicsPDFRendererContext/beginPage(withBounds:pageInfo:)) method on the supplied [`UIGraphicsPDFRendererContext`](/documentation/UIKit/UIGraphicsPDFRendererContext).

```objc
NSData *pdf = [renderer PDFDataWithActions:^(UIGraphicsPDFRendererContext * _Nonnull context) {
  [context beginPage];
  NSDictionary *attributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:150]};
  NSString *text = @"Hello!";
  [text drawInRect:CGRectMake(0, 0, 500, 200) withAttributes:attributes];}];  
```

The drawing actions closure takes a single argument of type [`UIGraphicsPDFRendererContext`](/documentation/UIKit/UIGraphicsPDFRendererContext). This provides access to some high-level drawing functions, such as [`fill(_:)`](/documentation/UIKit/UIGraphicsRendererContext/fill(_:)) through the [`UIGraphicsRendererContext`](/documentation/UIKit/UIGraphicsRendererContext) superclass.

> Note:
> This code uses a drawing method on <doc://com.apple.documentation/documentation/Foundation/NSString>. If you want to create a PDF with more text, consider using [TextKit](https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/iPhoneOSTechOverview/iPhoneOSTechnologies/iPhoneOSTechnologies.html#//apple_ref/doc/uid/TP40007898-CH3-SW11) or [Core Text](https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/LowerLevelText-HandlingTechnologies/LowerLevelText-HandlingTechnologies.html#//apple_ref/doc/uid/TP40009542-CH15-SW3), both of which provide extensive text layout functionality.

The above code creates the following result:

![Image of a PDF open in Preview, with the word “Hello!” rendered in large, black lettering in the top-left.](images/com.apple.uikit/media-2864001@2x.png)

### Adding pages

Add multiple pages to your PDF through repeated calls to the [`beginPage()`](/documentation/UIKit/UIGraphicsPDFRendererContext/beginPage()) method on the [`UIGraphicsPDFRendererContext`](/documentation/UIKit/UIGraphicsPDFRendererContext) provided to the drawing block.

```objc
NSData *pdf = [renderer PDFDataWithActions:^(UIGraphicsPDFRendererContext * _Nonnull context) {
  NSDictionary *attributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:150]}; 
  for (int page = 1; page < 4; page++) {
    [context beginPage];
    NSString *text = [NSString stringWithFormat:@"Page %d", page];
    [text drawInRect:CGRectMake(0, 0, 500, 200) withAttributes:attributes];
  }}];
```

Use the [`beginPage(withBounds:pageInfo:)`](/documentation/UIKit/UIGraphicsPDFRendererContext/beginPage(withBounds:pageInfo:)) method instead of the [`beginPage()`](/documentation/UIKit/UIGraphicsPDFRendererContext/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.

![Screenshot from Preview showing a 3-page PDF. Each page contains large black lettering which details the current page number.](images/com.apple.uikit/media-2864003@2x.png)

### 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:)`](/documentation/UIKit/UIGraphicsPDFRendererContext/addDestination(withName:at:)) method on [`UIGraphicsPDFRendererContext`](/documentation/UIKit/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:)`](/documentation/UIKit/UIGraphicsPDFRendererContext/setDestinationWithName(_:for:)) method on [`UIGraphicsPDFRendererContext`](/documentation/UIKit/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.

```objc
NSData *pdf = [renderer PDFDataWithActions:^(UIGraphicsPDFRendererContext * _Nonnull context) {
  NSDictionary *attributes = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:150]};
    
  NSString *nextPage = @"Next Page ↠";
  CGRect nextPageRect = CGRectMake(350, 250, 150, 40);
  NSDictionary *nextPageAttributes = @{
    NSFontAttributeName : [UIFont systemFontOfSize:25],
    NSBackgroundColorAttributeName : [UIColor redColor],
    NSForegroundColorAttributeName : [UIColor whiteColor]
  };
  for (int page = 1; page < 4; page++) {
    [context beginPage];
    NSString *pageNumber = [NSString stringWithFormat:@"Page %d", page];
    [pageNumber drawInRect:CGRectMake(0, 0, 500, 200) withAttributes:attributes];
    [nextPage drawInRect:nextPageRect withAttributes:nextPageAttributes];
      
    [context addDestinationWithName:[NSString stringWithFormat:@"page-%d", page]
                            atPoint:CGContextConvertPointToDeviceSpace(context.CGContext, CGPointZero)];
    [context setDestinationWithName:[NSString stringWithFormat:@"page-%d", page+1]
                            forRect:CGContextConvertRectToDeviceSpace(context.CGContext, nextPageRect)];
  }
}];
```

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 ``doc://com.apple.uikit/documentation/UIKit/UIGraphicsPDFRendererContext/addDestination(withName:at:)`` and ``doc://com.apple.uikit/documentation/UIKit/UIGraphicsPDFRendererContext/setDestinationWithName(_:for:)`` methods on ``doc://com.apple.uikit/documentation/UIKit/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 <doc://com.apple.documentation/documentation/CoreGraphics/CGContext/userSpaceToDeviceSpaceTransform> property on <doc://com.apple.documentation/documentation/CoreGraphics/CGContext>, as shown in the code.

The above code results in the following PDF:

![Screenshot from Preview showing a 3-page PDFs with red links entitled “Next Page” at the bottom-right of each page.](images/com.apple.uikit/media-2864256@2x.png)

## Topics

### Creating a PDF renderer

[`-  initWithBounds:format:`](/documentation/UIKit/UIGraphicsPDFRenderer/init(bounds:format:))

Creates a new graphics renderer with the specified bounds and format.

### Managing the PDF data

[`-  PDFDataWithActions:`](/documentation/UIKit/UIGraphicsPDFRenderer/pdfData(actions:))

Creates a PDF from a set of drawing instructions and returns it as a data object.

[`-  writePDFToURL:withActions:error:`](/documentation/UIKit/UIGraphicsPDFRenderer/writePDF(to:withActions:))

Creates a PDF from a set of drawing instructions and saves it to a specified URL.

[`DrawingActions`](/documentation/UIKit/UIGraphicsPDFRenderer/DrawingActions)

A closure for drawing PDF content.

## Relationships

### Conforms To

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`UIGraphicsRenderer`](/documentation/UIKit/UIGraphicsRenderer)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)