Printing Pdf using AirPrint causes small output

点点圈 提交于 2019-12-21 02:38:22

问题


I trying to print a pdf with UIPrintInteractionController that it load in the UIWevView. The good news is that i can print the bad is that the output of the print is to small.

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

the output:


回答1:


Further to @crescendolls response, here is a complete piece of code to convert PDF in your webview to data, and print it accordingly.

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



回答2:


If found the solution by avoid printing form IUWebView . I directly download the PDF in NSData format.

_data represente the NSData that i download by a request.

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



回答3:


try with this peace of code..

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

}



来源:https://stackoverflow.com/questions/23155580/printing-pdf-using-airprint-causes-small-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!