开发者

iOS Air print for UIwebview

Can any one guide me how to print the contents of开发者_运维知识库 my UIWebview,

FOR EX : - i would like to print my doc,xls,ppt file from UIWebview to print the contents.

Please get some links or sample code to solve this problem

Thanks in advance


UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = webView.request.URL.absoluteString;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.showsPageRange = YES;
pic.printFormatter = webView.viewPrintFormatter;
[pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
    // indicate done or error
}];

A more extensive sample on Apple's dev site.


To print contents of UIWebview, view formatters are required. I have pasted the code below.

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
//pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"google.com";
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;

// Webvied print
NSData *mydata=[NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://www.google.com"]];
// Use this webview if your content is not loaded into webview, if webview already exists then give its reference here
UIWebView *webview = [[UIWebView alloc] initWithFrame: CGRectZero];
[webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @"http://www.google.com"]]];
[webview loadData:mydata MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL: [NSURL URLWithString: @"http://www.google.com"]];
UIViewPrintFormatter *formatter = [webview viewPrintFormatter];
pic.printFormatter = formatter;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"Printing could not complete because of error: %@", error);
        }
};

[pic presentAnimated:YES completionHandler:completionHandler];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜