本文主要是介绍[Trans]AirPrint in webview transfer to PDF,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://www.cnblogs.com/kiss007/archive/2012/06/26/2563662.html
实例1:
Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable]){printInteraction = [printInteractionController sharedPrintController];printInteraction.delegate = self;UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];printInfo.duplex = UIPrintInfoDuplexLongEdge;printInfo.outputType = UIPrintInfoOutputGeneral;printInfo.jobName = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];printInteraction.printInfo = printInfo;printInteraction.showsPageRange = YES;UIViewPrintFormatter *formatter = [webView viewPrintFormatter];printInteraction.printFormatter = formatter;[printInteraction presentFromBarButtonItem:actionButtonItemanimated:YEScompletionHandler:^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {}];}
实例2:
Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable]){NSURL *fileURL = document.fileURL; // Document file URLprintInteraction = [printInteractionController sharedPrintController];if ([printInteractionController canPrintURL:fileURL] == YES) // Check first{UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];printInfo.duplex = UIPrintInfoDuplexLongEdge;printInfo.outputType = UIPrintInfoOutputGeneral;printInfo.jobName = document.fileName;printInteraction.printInfo = printInfo;printInteraction.printingItem = fileURL;printInteraction.showsPageRange = YES;if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){[printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:^(UIPrintInteractionController *pic, BOOL completed, NSError *error){#ifdef DEBUGif ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);#endif}];}else // Presume UIUserInterfaceIdiomPhone{[printInteraction presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic, BOOL completed, NSError *error){#ifdef DEBUGif ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);#endif}];}}}
利用UIPrintPageRenderer将html转换成pdf
Boss让我调查一下把html转换成PDF的方法,google之,有很多,都是用webview加载,截屏,转换。
于是去向Boss报告,结果他说这样转换成的pdf是纯图片的,不能对文本进行操作了,达不到想要的效果。
无奈,继续google。结果,再一次领略了stackoverflow的强大,最终找到一个解决方案:http://stackoverflow.com/q/9528658/966127
实现过程,子类化UIPrintPageRenderer
- (CGRect) paperRect {if (!_generatingPdf)return [super paperRect];return UIGraphicsGetPDFContextBounds(); }- (CGRect) printableRect {if (!_generatingPdf)return [super printableRect];return CGRectInset( self.paperRect, 20, 20 ); }- (NSData*) printToPDF {_generatingPdf = YES;NSMutableData *pdfData = [NSMutableData data];UIGraphicsBeginPDFContextToData( pdfData, CGRectMake(0, 0, 792, 612), nil ); // letter-size, landscape[self prepareForDrawingPages: NSMakeRange(0, 1)];CGRect bounds = UIGraphicsGetPDFContextBounds();for ( int i = 0 ; i < self.numberOfPages ; i++ ){UIGraphicsBeginPDFPage();[self drawPageAtIndex: i inRect: bounds];}UIGraphicsEndPDFContext();_generatingPdf = NO;// NSString* filename = @"/Volumes/Macintosh HD 2/test.pdf"; // [pdfData writeToFile: filename atomically: YES];return pdfData; }
UIViewPrintFormatter *viewFormatter = [htmlWebView viewPrintFormatter];[ppRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];NSData *pdfData = [ppRenderer printToPDF];NSString *pdfPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.pdf"];[pdfData writeToFile:pdfPath atomically:YES];
这篇关于[Trans]AirPrint in webview transfer to PDF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!