最近在作的一个iOS项目有个打印 pdf的功能。查找资料发现苹果早在 iOS 4.2 就有了 AirPrint 功能。网上关于AirPrint 的资料很少,因此就写了这篇博文。 下面就和你们分享一下本身的学习收获。html
an array of ready-to-print images and PDF documents: 一组图片文件和PDF文件。swift
a single image or PDF document: 一张图片或是一个pdf文件。、api
an instance of any of the built-in print formatter classes: 打印格式化者的实例。(简单文本,html文档,某些View显示的内容)。bash
a custom page renderer: 自定义页渲染者。 注释: 1,2 很简单,就不详细解释了,3,4姑且这样翻译了,不懂不要紧, 继续向下看。。。app
UIPrintInfo *printInfo: 打印任务的信息。dom
UIPrintPaper * printPaper : 打印内容的区域。iphone
delegate: 遵照UIPrintInteractionControllerDelegate 协议的代理。学习
最重要的就是制定须要打印的内容: printingItem , printingItems, printFormatter, printPageRenderer。 四个属性都是用来指定要打印的内容的。 这四个参数是互斥的, 也就是说只要一个赋值, 其余三个参数就得是nil. 很容易理解,一个打印任务, 不能同时干多个活呀。 这里若是使用 swift的枚举,就很容易理解了。 这里提到的四个关于内容的属性, 和 第4 点是关联起来的。 下面这张表是对应关系: 测试
建立 UIPrintInteractionController 实例。ui
建立UIPrintInfo 实例。 并 配置参数 output type(输出类型), print orientation(打印方向), job name(打印工做标识), 而后赋值给UIPrintInteractionController 实例的 printInfo属性。
给delegate 属性赋值, 赋的值必须遵照 UIPrintInteractionControllerDelegate 协议。 这个代理能够 响应 printing options界面的显示和消失, 打印工做的开始和结束 等。
指定要打印的内容。 也就是指定 printingItem , printingItems, printFormatter, printPageRenderer. 参数的其中一个。
当你使用 printPageRenderer. 时状况会复杂一些。 你能够绘制每一页的header, footer, 内容。 这是你须要本身计算页数。 另外, 你也能够建立一个或多个 UIPrintFormatter实例, 经过 addPrintFormatter:startingAtPageAtIndex: 或者 printFormatters参数 赋值给 printPageRenderer.实例。 这种状况下不须要本身计算多少页。
最后就是显示显示出printing options 界面了。 方法: 在iPad上: presentFromBarButtonItem:animated:completionHandler: 或者 presentFromRect:inView:animated:completionHandler:; 在手机上: presentAnimated:completionHandler:
AirPrint能够直接打印一些内容。 这些内容是 NSData, NSURL, UIImage, and ALAsset 类的实例, 可是这些实例的内容, 或者引用的类型(NSURL)必须是 image 或者pdf.
对于 image来讲, NSData, NSURL, UIImage, and ALAsset 类型均可以的。 对于PDF, 只能使用 NSData, NSURL。 而后须要将这些数据实例直接赋值 给 UIPrintInteractionController实例的 printingItem 或者 printingItems 属性。
打印pdf:
- (IBAction)printContent:(id)sender {
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
if (pic && [UIPrintInteractionController canPrintData: self.myPDFData] ) {
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [self.path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = self.myPDFData;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
self.content = nil;
if (!completed && error)
NSLog(@"FAILED! due to error in domain %@ with error code %u",
error.domain, error.code);
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:self.printButton animated:YES
completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
复制代码
系统提供了三个 Print Formatters类, 分别是:
UIViewPrintFormatter—automatically lays out the content of a view over multiple pages. To obtain a print formatter for a view, call the view’s viewPrintFormatter method. Not all built-in UIKit classes support printing. Currently, only the view classes UIWebView, UITextView, and MKMapView know how to draw their contents for printing. View formatters should not be used for printing your own custom views. To print the contents of a custom view, use a UIPrintPageRenderer instead.
UISimpleTextPrintFormatter—automatically draws and lays out plain-text documents. This formatter allows you to set global properties for the text, such a font, color, alignment, and line-break mode.
UIMarkupTextPrintFormatter—automatically draws and lays out HTML documents.
- (IBAction)printContent:(id)sender {
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = self.documentName;
pic.printInfo = printInfo;
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc]
initWithMarkupText:self.htmlString];
htmlFormatter.startPage = 0;
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
pic.printFormatter = htmlFormatter;
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}
复制代码
- (void)printWebPage:(id)sender {
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(!completed && error){
NSLog(@"FAILED! due to error in domain %@ with error code %u",
error.domain, error.code);
}
};
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [urlField text];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter];
viewFormatter.startPage = 0;
controller.printFormatter = viewFormatter;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler];
}else
[controller presentAnimated:YES completionHandler:completionHandler];
}
复制代码
这部份内容是最复杂的了, 感受不怎么用,暂且不深究了, 你们若是项目须要, 本身看文档吧。
到这里,我学习到的 AirPrint的主要技术点已经和你们分享完了。基本能够搞定大部分的需求。 若是那里说错了, 很是欢迎你们能给提出。 另外刚开始写技术博客, 经验不足, 但愿你们多提宝贵意见。