iOS UIPrintInteractionController打印
- (void)printData{ //为打印做准备,创建一个指向sharedPrintController的引用 UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController]; printer.delegate = self; //配置打印信息 UIPrintInfo *Pinfo = [UIPrintInfo printInfo]; Pinfo.outputType = UIPrintInfoOutputGeneral;//可打印文本、图形、图像 Pinfo.jobName = @"Print for xiaodui";//可选属性,用于在打印中心中标识打印作业 Pinfo.duplex = UIPrintInfoDuplexLongEdge;//双面打印绕长边翻页,NONE为禁止双面 Pinfo.orientation = UIPrintInfoOrientationPortrait;//打印纵向还是横向 // Pinfo.printerID = @"";//指定默认打印机,也可以使用UIPrintInteractionControllerDelegate来知悉 printer.printInfo = Pinfo; //设置页面范围 打印文字 // UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"哈护手霜按时哈哈"]; // textFormatter.startPage = 0;//指定从哪一张开始打印0代表第一张 // textFormatter.contentInsets = UIEdgeInsetsMake(36, 36, 36, 36);//72相当于1英寸,这样设置上下左右的边距都为0.5英寸 // textFormatter.maximumContentWidth = 504;//(72x7.5)相当于打印宽度为7英寸 // printer.printFormatter = textFormatter;
/*
- AirPrint可以直接打印一些内容。 这些内容是 NSData, NSURL, UIImage, and ALAsset 类的实例, 但是这些实例的内容, 或者引用的类型(NSURL)必须是 image 或者pdf.
- 对于 image来说, NSData, NSURL, UIImage, and ALAsset 类型都可以的。 对于PDF, 只能使用 NSData, NSURL。 然后需要将这些数据实例直接赋值 给 UIPrintInteractionController实例的 printingItem 或者 printingItems 属性。
*/
// printer.printingItem = [UIImage imageNamed:@"LaunchImage"]; printer.printingItems = @[[UIImage imageNamed:@"welcome_page2"], [UIImage imageNamed:@"LaunchImage"], [UIImage imageNamed:@"welcome_page1"],]; printer.showsPageRange = NO; [printer presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) { if (!completed && error) { NSLog(@"Error"); } }]; }
打印webView视图
初始化webView
- (UIWebView *)printWebView { if (!_printWebView) { _printWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)]; _printWebView.backgroundColor = HexRGB(0xf9f9f9); _printWebView.delegate = self; _printWebView.scalesPageToFit = YES; }
return _printWebView; }
加载视图
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.printWebView loadRequest:request];
在加载完成中调用打印方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{ [self printWebViewPage]; } - (void)printWebViewPage{ UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if(error){ NSLog(@"FAILED! due to error in domain %@ with error code %ld-- %@", error.domain, (long)error.code, completionHandler); } }; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = @"Print for xiaodui";; printInfo.duplex = UIPrintInfoDuplexLongEdge; controller.printInfo = printInfo; controller.showsPageRange = YES; UIViewPrintFormatter *viewFormatter = [self.printWebView viewPrintFormatter]; viewFormatter.startPage = 0; controller.printFormatter = viewFormatter; [controller presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
if (!completed && error) { NSLog(@"Error"); } }]; }
如图: