我有以下代码来打印简单文本。我使用的是HP Officejet 4630系列。但我收到UIPrintErrorDomain错误4以及错误3。
知道为什么吗?
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = @"test";
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
UISimpleTextPrintFormatter *formatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"testing 123"];
formatter.contentInsets = UIEdgeInsetsMake(72, 72, 72, 72);
pic.printFormatter = formatter;
NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:@"printer"];
UIPrinter *printer = [UIPrinter printerWithURL:[NSURL URLWithString:url]];
[pic printToPrinter:printer completionHandler:^(UIPrintInteractionController * __nonnull printInteractionController, BOOL completed, NSError * __nullable error) {
NSLog(@"error -> %@", error);
}];
我有正确的打印机网址。 我收到了以下错误。
ipp://HPD0...15287.local.:631/ipp/print: startJob: Unable to connect to printd: Bad file descriptor
Error Domain=UIPrintErrorDomain Code=4 "The operation couldn’t be completed. (UIPrintErrorDomain error 4.)"
答案 0 :(得分:0)
也浪费了一些时间寻求解决方案。 发现了这样一个:
private selectPrinter() {
const printPicker = UIPrinterPickerController.printerPickerControllerWithInitiallySelectedPrinter(null);
const view = utils.ios
.getter(UIApplication, UIApplication.sharedApplication).keyWindow.rootViewController.view;
const theFrame = frame.topmost().currentPage.frame;
printPicker.presentFromRectInViewAnimatedCompletionHandler(
theFrame, view, true, (printerPicker, userDidSelect, error) => {
if (userDidSelect) {
this.someService.printerUrl = printerPicker.selectedPrinter.URL;
} else {
setTimeout(() => { this.selectPrinter(); }, 4);
}
},
);
}
const printer = UIPrinter.printerWithURL(this.someService.printerUrl);
printer.contactPrinter((available) => {
if (available) {
controller.printToPrinterCompletionHandler(printer, callback);
} else {
alert("Printer Not Found");
}
});
希望它将对某人有所帮助