首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >uiWebView打印pdf

uiWebView打印pdf
EN

Stack Overflow用户
提问于 2014-11-13 13:27:11
回答 1查看 291关注 0票数 1

在uiwebview内部,打印pdf文档的好方法是什么?

pdf可以通过url访问,也可以加载到iframe中。

使用标准的javascript widnow.print()函数将无法工作。

我正在考虑使用javascript桥,例如:

代码语言:javascript
运行
复制
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *URL = [request URL]; 

    if ([[URL scheme] isEqualToString:@"native"]) { 
        NSString *urlString = [[request URL] absoluteString];
        NSArray *urlParts = [urlString componentsSeparatedByString:@":"];
        NSString *cmd = [urlParts objectAtIndex:1];

        if ( [cmd isEqualToString:@"printPdf"] ) {
            //  [self dosomething];
        }
    }
    return YES;
}

此时,我需要某种xcode函数,它接受到pdf的路径,并将其发送给airPrinter。

这样做好吗?我正在寻找如何在uiWebView中打印pdf的例子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-20 18:18:02

由于我已经赢得了无选票和无回应的卷尾草徽章,我将张贴我的解决方案。

这将获取pdf文档并打开airPrint对话框--所有这些都在uiWebView中。

因此,如果IOS只允许javascript window.print()在uiWebView内部运行,我的应用程序就不会在应用程序商店中等待批准和重新发布。

总之,这里有一个可行的解决方案:

代码语言:javascript
运行
复制
- (void)printInit:(NSString *)parm  {

    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    if(!controller){
        NSLog(@"Couldn't get shared UIPrintInteractionController!");
        return;
    }

    NSString *base = @"https://someurl.com/";
    NSString *ustr = [base stringByAppendingString:parm];

    //NSURL *url = [NSURL fileURLWithPath:ustr];
    NSURL *url = [NSURL URLWithString:ustr];    
    NSData *thePdf = [NSData dataWithContentsOfURL:url];

    controller.printingItem = thePdf;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"PDFDoc";
    controller.printInfo = printInfo;

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
             NSLog(@"FAILED! error = %@",[error localizedDescription]);
        }
    };

    CGRect rect = CGRectMake(310, 5, 100, 5);
    [controller presentFromRect:rect inView:self.webView animated:YES completionHandler:completionHandler];
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26909999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档