Generate PDFs from Templates on iOS

PSPDFKit for iOS enables you to create a new PDF document from a template. Our SDK ships with a predefined list of page templates, which are ready to use:

Template
PageTemplate.Identifier
Blank .blank
Dot 5mm .dot5mm
Grid 5mm .grid5mm
Line 5mm .lines5mm
Line 7mm .lines7mm
Image .image

In addition to these built-in templates, you can use the PageTemplate class to build customized page templates from your own PDF documents.

ℹ️ Note: This feature requires the Document Editor component to be enabled in your license.

The PageTemplate Class

PageTemplate has three initializers you can use depending on what you’re trying to achieve:

ℹ️ Note: Creating a tiled pattern page template requires the source document to be exported correctly. This means that the source PDF needs to contain a pattern itself. If a PageTemplate is instantiated using the tiled pattern initializer and the source document doesn’t contain a pattern, the rendering will fail silently.

Check out DocumentEditorCustomTemplatesExample.swift in the Catalog app to see PageTemplate in action.

Custom Tiled Templates

Many use cases for page templates require the background being tiled (or patterned).

A page is considered tiled if there are one or more images repeated on the page.

Template Example

For a PDF to be able to work as a source for a tiled page template using the new PageTemplate(tiledPatternFrom:sourcePageIndex:) API, it has to have actual pattern path information embedded.

To accomplish this, you can use Adobe Illustrator or any other vector editing tool.

When creating your own patterns, please consider the following points:

  1. What’s rendered on the page is the path information embedded in the PDF and not the actual PDF.

  2. If your custom pattern needs certain spacing between tiles, that information needs to be included within the pattern information as well. Currently, there’s no way to specify spacing between tiles from the PSPDFKit API.

Click here to download a custom sample template.

Using PageTemplate

Create a new PDFNewPageConfiguration object with a blank PageTemplate. Use the PageTemplate.blank convenience initializer to simplify the code. The builder block of PDFNewPageConfiguration’s initializer enables you to customize various properties of the page, such as the background color or page size:

// Create a configuration for an empty A4 size page with a white background color.
let newPageConfiguration = PDFNewPageConfiguration(pageTemplate: PageTemplate.blank) {
    $0.backgroundColor = UIColor.white
    $0.pageSize = CGSize(width: 595, height: 842) // A4 in points.
}

There are two ways to create the PDF document from newPageConfiguration: using the Document Editor, or using Processor. Both of them are explained in detail below.

Using Document Editor

Document Editor is a PSPDFKit component comprised of a set of MVC classes. It provides you and your users with a whole host of page editing features, including new page creation, page duplication, copying and pasting, reordering, rotation, deletion, and the creation of new documents from a subset of selected pages.

To create an empty PDF, initialize PDFDocumentEditor without a PDFViewController. You can then use the regular PDFDocumentEditor API to add pages. When done, save the new document to a location on the file system:

guard let documentEditor = PDFDocumentEditor(document: nil) else { return }
documentEditor.addPages(in: NSRange(location: 0, length: 1), with: newPageConfiguration)

// Save to a new PDF file.
documentEditor.save(toPath: saveToPath) { document, error in
    if let error = error {
        print("Error saving document. Error: \(error)")
    }
}

Using Processor

The Processor class, as the name suggests, allows you to process PDF documents. These operations include document creation, merging, and modification.

After configuring the page as described in the first section, it’s added to Processor.Configuration, which in turn is used to create a Processor object that will generate the actual PDF.

The example below shows how to generate a PDF using the Processor API:

let configuration = Processor.Configuration()
configuration.addNewPage(at: 0, configuration: newPageConfiguration)

// Save to a new PDF file.
let processor = Processor(configuration: configuration, securityOptions: nil)
try processor.write(toFileURL: outputFileURL)

See NewDocumentCreationExample from the Catalog app for a complete example of how to generate a new PDF.