Contents

UIGraphicsPDFRenderer

A graphics renderer for creating PDFs.

Declaration

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 object to specify nondefault parameters the renderer should use to create its context.

  2. 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.

  3. 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.

  4. Provide Core Graphics drawing instructions within the closure associated with your chosen method, as shown in Creating a PDF with a PDF renderer.

  5. Optionally, you can create a multi-page PDF, using the approach shown in Adding pages.

  6. 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.

The above code creates the following result:

[Image]

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.

[Image]

You can create internal links, known as destinations, in PDFs. A complete link has two components:

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.

The above code results in the following PDF:

[Image]

Topics

Creating a PDF renderer

Managing the PDF data

See Also

Graphics contexts