2011-07-01 4 views
7

Quelqu'un peut-il me guider comment imprimer le contenu de mon UIWebView,iOS impression d'air pour UIWebView

Ex: - Je voudrais imprimer mon doc, xls, fichier ppt de UIWebView pour imprimer le contenu.

S'il vous plaît obtenir des liens ou des exemples de code pour résoudre ce problème

Merci à l'avance

+1

Je serais très surpris si UIWebView pouvait afficher des fichiers Word, Excel ou Powerpoint, qu'est-ce qui vous fait penser qu'il peut? – DarkDust

+9

@DarkDust http://developer.apple.com/library/ios/#qa/qa1630/_index.html – Dolbz

+0

@Dolbz: Merci, je ne sais pas. – DarkDust

Répondre

16
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 
}]; 

Une sample plus étendue sur le site d'Apple dev.

0

Pour imprimer le contenu d'UIWebview, vous devez afficher les formateurs. J'ai collé le code ci-dessous.

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];