使用AirPrint打印Pdf会导致输出很小

时间:2014-04-18 13:53:36

标签: ios objective-c printing uiwebview uiprintinteractioncntrler

我尝试打印UIPrintInteractionController加载UIWevView的pdf。好消息是,我可以打印坏的是打印输出很小。

any help would be appreciated :)


- (IBACTION) printPDF {


    if ((!_webView)) return;
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if(!completed && error){
            NSLog(@"FAILED! due to error in domain %@ with error code %ld",
                  error.domain, (long)error.code);
        }
    };
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    controller.printInfo = printInfo;
    controller.showsPageRange = YES;
    _webView.scalesPageToFit = NO;
    UIViewPrintFormatter *viewFormatter = [_webView viewPrintFormatter];
    viewFormatter.startPage = 0;
     controller.printFormatter = viewFormatter;



    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        /*        [controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler]; */
    }else
        [controller presentAnimated:YES completionHandler:completionHandler];
}

输出:

enter image description here

3 个答案:

答案 0 :(得分:5)

除了@crescendolls响应之外,这里还有一段完整的代码,用于将webview中的PDF转换为数据,并相应地进行打印。

-(void)printReport {
    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;

    //does not display PDF appropriately
    //pic.printFormatter = webView.viewPrintFormatter;

    //does display PDF appropriately
    NSCachedURLResponse* webResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:[webView request]];
    NSData* webData = [webResponse data];
    pic.printingItem = webData;

    [pic presentFromBarButtonItem:self.printBarButton animated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
        // indicate done or error
    }];
} 

答案 1 :(得分:0)

如果找到解决方案避免打印表单IUWebView。我直接 NSData 格式下载 PDF

_data表示我通过请求下载的NSData。

 if (_data) {
    UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];

    printController.delegate = self;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    //printInfo.jobName = [path lastPathComponent];
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    printController.printInfo = printInfo;
    printController.showsPageRange = YES;
    printController.printingItem = _data;

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"FAILED! due to error in domain %@ with error code %lu", error.domain, (long)error.code);
        }
    };

    [printController presentAnimated:YES completionHandler:completionHandler];
}

答案 2 :(得分:0)

尝试这种代码的和平..

-(IBAction)printPDF
 {
        _webView.scalesPageToFit=YES;
        UIImage *img=[self imageWithView:_webView];

        UIPrintInteractionController *pic = [UIPrintInteractionController
                                             sharedPrintController];

        UIPrintInfo *printInfo = [UIPrintInfo printInfo];

        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.duplex = UIPrintInfoDuplexLongEdge;

        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        pic.printingItem = img;
        pic.showsPageRange = YES;


        UIPrintInteractionCompletionHandler completionHandler =
        ^(UIPrintInteractionController *printController, BOOL completed,
          NSError *error) {
            if(!completed && error)
            {
                NSLog(@"Print failed - domain: %@ error code %u", error.domain,
                      error.code);
            }
        };

}