Open a PDF from In-Memory Data in iOS

PSPDFKit for iOS enables you to open in PDFs and images (PNG, JPEG, and TIFF) represented as in-memory data blobs or byte buffers, allowing you to operate on documents without ever storing them on a device’s file system. This article shows how to open a PDF document from a Data instance.

In this example you’ll load the PDF data from a text file that contains a PDF serialized as a Base64 string.

First, you need to add the data file for the PDF document you want to display to your application by dragging it into your project. On the dialog that’s displayed, select Finish to accept the default integration options. You can use this data file as an example.

Then, create the Data instance to load your PDF document and display the view controller. This can be done in a button action handler, table view cell selection delegate, or similar:

// Update to use your document name and location.
let fileURL = Bundle.main.url(forResource: "Document", withExtension: "txt")!

// Load the PDF data from the Base64 string.
guard let dataString = try? String(contentsOf: fileURL),
           let data = Data(base64Encoded: dataString) else {
    print("Could not load data.")

// Instantiate a data container provider object with the data payload.
let dataContainerProvider = DataContainerProvider(data: data!)

// Create the document with an array of data providers.
let document = Document(dataProviders: [dataContainerProvider])

// The configuration closure is optional and allows additional customization.
let pdfController = PDFViewController(document: document) {
	$0.isPageLabelEnabled = false

// Present the PDF view controller within a `UINavigationController` to show built-in toolbar buttons.
present(UINavigationController(rootViewController: pdfController), animated: true)

For more details about opening a document from in-memory data, please refer to DocumentDataProviderPDFFromDataExample.swift in the PSPDFKit Catalog app.