Generate Blank PDFs on iOS

PSPDFKit for iOS can create a blank PDF document in a couple of different ways.

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

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.