UIPrintInteractionController Not Presenting Multiple Pages

631 Views Asked by At

This issue has just appeared with iOS 13 and was not a problem prior. If I am presenting the UIPrintInteractionController controller with a single image, everything works fine. If I submit with more than one image, the print controller will not be displayed and instead I will get an error that states: Warning: Attempt to dismiss from view controller <UIViewController: 0x7fefcc4e7ab0> while a presentation or dismiss is in progress!

Below is the code in question. So again, if printingItems contains more than 1 element (which is the entire point of it), the controller won't display and the success portion of the completion handler will return false. Was not an issue in iOS 12.This is running on an iPad.

private func print(finalPageImages:[UIImage]) {
    let printInfo = UIPrintInfo(dictionary: nil)
    printInfo.jobName = "job name"
    printInfo.outputType = .general
    printInfo.duplex = .none
    printInfo.orientation = .landscape

    let printController = UIPrintInteractionController.shared
    printController.showsNumberOfCopies = false
    printController.printInfo = printInfo
    printController.printingItems = finalPageImages

    printController.present(from: self.printButton, animated: true) { (controller, success, error) in
        guard error == nil else {
            Utilities.displayAlert(title: "Print Error", msg: error!.localizedDescription, controller: self)
            return
        }
        if success {
            Utilities.displayAlert(title: "Print Status", msg: "Your Shelf Talkers are printing.", controller: self, completion: { (action) in

            })
        } else {
            Utilities.displayAlert(title: "Print Error", msg: "There was a problem with this print job. Please try again.", controller: self)
        }
    }
}
1

There are 1 best solutions below

0
On

I was also facing the same issue with multiple pdf files, so I just merged all the pdfs into one pdf and used

printController.printingItem = mergeAllPdfs().dataRepresentation()

instead of

printController.printingItems = [myPdf1.data, myPdf2.data, myPdf3.data]

Its a work around to solve the issue for now. Hope its helps.

func mergeAllPdfs() -> PDFDocument? {
    // get the first pdfData
    guard let firstPdf = allPDFs.first?.data, let pdfDocument = PDFDocument(data: firstPdf) else {
        return nil
    }
    // create new PDFDocument
    let newPdfDocument = PDFDocument()

    // insert all pages of first document
    for p in 0..<pdfDocument.pageCount {
        if let page = pdfDocument.page(at: p) {
            newPdfDocument.insert(page, at: newPdfDocument.pageCount)
        }
    }

    // loop for number of documents instead first one (which is allready added)
    for count in 1..<allPDFs.count {
        guard let nextPdfData = allPDFs[count].data, let nextPdf = PDFDocument(data: nextPdfData) else {
            return nil
        }
        for q in 0..<nextPdf.pageCount {  // all pages of a next document
            if  let page = nextPdf.page(at: q) {
                newPdfDocument.insert(page, at: newPdfDocument.pageCount)
            }
        }
    }
    return newPdfDocument
}