UIPrintInteractionController inconsistence on repeated print action

126 Views Asked by At

I am trying to print label in a swiftUI using airPrint My label is a html Code this is what I am doing :

let printController = UIPrintInteractionController.shared

let printInfo = UIPrintInfo(dictionary: nil)
printInfo.outputType = .photo
printInfo.orientation = .landscape
printController.printInfo = printInfo

let formatter = UIMarkupTextPrintFormatter(markupText: [HtmlCode])
formatter.perPageContentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
formatter.maximumContentWidth = 4 * 72.0
formatter.maximumContentHeight = 1.5 * 72

printController.printFormatter = formatter
let printer = UIPrinter(url: URL(string: "[PrinterURL]")!)
printController.print(to: printer)

I am getting mixte result, on the first job the label is perfect. On the second job and every one after that, inside the same app instance, the label is incorrect.

the incorrect label start printing after letting 1.5 label pass and is printing at a bigger format then the first one. So the problem seems to be that the format of the paper is invalid on the second print, or that there is a zoom factor that is added on the second job

Printing the label with the presenter works perfectly every time, but i need to print automatically so I am using the print function

Correct label Invalid label

I try to push the papersize with the UIPrintInteractionControllerDelegate. I try to change the formatter, with different padding and margins I try to add multiple formatters I try to print the same label but in a pdf format but with no success

1

There are 1 best solutions below

0
Schmavery On

We ran into this problem with one of our apps recently. It seems that there is a bug/issue in iOS 16 that is causing it.

We took inspiration from the issue trying to fix the problem in Expo, where alexbeckwith suggested caching the UIPrinter between prints, as it gets set up properly on the first print. This fixed the problem for us.

let printer = self.cachedPrinters[printerURL] ?? UIPrinter(url: URL(string: printerURL)!)
self.cachedPrinters[printerURL] = printer

Hope that helps :)