5

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
iOS4Life
  • 226
  • 2
  • 13
  • Did you find a solution to you problem yet? Having the exact same issue over here. Its a shame that we have to deal with this stupid stuff. Observation: The PDFs actually get printed correctly. Checked with Printer Simulator. It is just the preview that does not show! – Josh Feb 08 '19 at 17:10
  • I also faced the same issue in swift, XCode 11 and iOS 13 > – Anbu.Karthik May 08 '20 at 04:08
  • 1
    Can you provide absolute path of `filePath1` and `filePath2`? – Adrian Bobrowski May 14 '20 at 16:33

1 Answers1

0

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)
            }
        }
    }
}
silly_cone
  • 137
  • 1
  • 8