7

谁能指导我如何打印我的 UIWebview 的内容,

FOR EX : - 我想从 UIWebview 打印我的 doc、xls、ppt 文件以打印内容。

请获取一些链接或示例代码来解决此问题

提前致谢

4

2 回答 2

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

Apple 开发网站上的更广泛示例。

于 2013-07-10T22:06:55.140 回答
0

要打印 UIWebview 的内容,需要查看格式化程序。我已经粘贴了下面的代码。

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];
于 2014-05-21T08:05:13.733 回答