Instantiate a Download from a Remote URL

PSPDFKit usually works best with PDF documents on the local file system of your device. There are several reasons for using local file system documents (these include performance, cache control, and battery impact).

However, if you have a PDF document that isn’t on the local file system, you can also instantiate a Document with a remote URL via Document(url:):

let document = Document(url: "https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf")
let controller = PDFViewController(document: document)

This will download the remote document and cache it. If you’re interested in how this works exactly, check out our blog post about it. You can also disable caching by doing the following:

// By default, downloads use this custom large cache to ensure files are being updated correctly.
// Set the cache to `nil` to disable this caching.
// https://pspdfkit.com/blog/2020/downloading-large-files-with-urlsession/
URLDataProvider.cache = nil

// Ensure the temporary file does not exist.
try? FileManager.default.removeItem(at: URLDataProvider.defaultTargetURL(for: url)!)

Files downloaded from a remote URL using this method are read-only by default, and annotations cannot be saved, as the source file could change at any time.

Information

Refer to RemoteDocumentURLExample.swift in the PSPDFKit Catalog for a complete example.

Downloading Documents Using URLSession

If you want to annotate a remote document or make changes to it, you can use URLSession to download the PDF. Then move it to a permanent location and display it using PDFViewController.

Create a URLSession object with the required configurations. Then, use the session to create a download task (URLSessionDownloadTask) with the remote URL of the PDF document. Before resuming the task, make sure you’ve set the session object’s delegate:

let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
let task = session.downloadTask(with: URL(string: "https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf")!)
task.resume()
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:[NSURL URLWithString:@"https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/pdf_reference_1-7.pdf"]];
[task resume];

Once the file is downloaded, the urlSession(_:downloadTask:didFinishDownloadingTo:) delegate method is called where the location object is a temporary file URL. Hence it should be moved to a permanent location before opening it with PDFViewController:

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    // The desired saving location of the file is `destinationFileURL`, which `PDFViewController` will later use for loading.
    try! FileManager.default.moveItem(at: location, to: destinationFileURL)
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSError *error;

    // The desired saving location of the file is `destinationFileURL`, which `PDFViewController` will later use for loading.
    [[NSFileManager defaultManager] moveItemAtURL:location toURL:destinationFileURL error:&error];
    if (error) {
        NSLog(@"%@", error.localizedDescription);
    }
}
Information

Refer to DocumentProgressExample.swift in the PSPDFKit Catalog for a complete example.