【问题标题】:uiWebView printing a pdfuiWebView 打印 pdf
【发布时间】:2015-01-10 15:56:43
【问题描述】:

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

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

使用标准的 javascript widnow.print() 函数将不起作用。

我正在考虑使用 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 的示例。

【问题讨论】:

    标签: pdf iframe printing uiwebview


    【解决方案1】:

    由于我因没有投票和没有回复而获得了风滚草徽章,所以我将发布我的解决方案。

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

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

    无论如何,这是一个可行的解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 2019-02-02
      • 2017-11-24
      • 2010-10-29
      • 2011-08-31
      相关资源
      最近更新 更多