UIPrintInteractionController printingItems not showing up / working

1.1k views Asked by At

Pretty straightforward...

I consider myself a fairly well-seasoned iOS developer these days, but this one seems like a glaring bug in iOS, unless I missed something.

Please see the code below.

The file paths point to two one-page PDFs.

What shows up is a Print interaction controller with no content to print.

If I instead only do 1 file at a time like this:

pc.printingItem = [NSURL fileURLWithPath:filePath1];

it works like a champ.

What am I missing here?!

UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputPhoto;
printInfo.orientation = UIPrintInfoOrientationLandscape;
pc.printInfo = printInfo;

pc.printingItems = @[[NSURL fileURLWithPath:filePath1], 
                     [NSURL fileURLWithPath:filePath2]];

[pc presentAnimated:YES completionHandler:completionHandler];

enter image description here

1

There are 1 answers

0
silly_cone On

I had the same issue and solved it by setting the printingItem property with raw data of my PDF file.

// Here is a class I created that works with `UIActivityViewController`,
// feel free to tweak to your needs.

final class CustomActivityViewController: UIActivityViewController {

    init(with filePath: URL, and fileData: Data?) {
        super.init(activityItems: [filePath], applicationActivities: [])
    
        completionWithItemsHandler = { activity, complete, items, error in
        
            if activity == .print {
            
                let printInfo = UIPrintInfo(dictionary: nil)
                printInfo.jobName = filePath.lastPathComponent
                printInfo.outputType = .general
            
                let printer = UIPrintInteractionController.shared
                printer.printingItem = fileData // <-- This is the cure.
                printer.printInfo = printInfo
            
                printer.present(animated: true, completionHandler: nil)
            }
        }
    }
}