iOS UIPrintInteractionController打印


- (void)printData{

    //為打印做准備,創建一個指向sharedPrintController的引用
    UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];
    printer.delegate = self;

    //配置打印信息
    UIPrintInfo *Pinfo = [UIPrintInfo printInfo];
    Pinfo.outputType = UIPrintInfoOutputGeneral;//可打印文本、圖形、圖像
    Pinfo.jobName = @"Print for xiaodui";//可選屬性,用於在打印中心中標識打印作業
    Pinfo.duplex = UIPrintInfoDuplexLongEdge;//雙面打印繞長邊翻頁,NONE為禁止雙面
    Pinfo.orientation = UIPrintInfoOrientationPortrait;//打印縱向還是橫向

    //    Pinfo.printerID = @"";//指定默認打印機,也可以使用UIPrintInteractionControllerDelegate來知悉
    printer.printInfo = Pinfo;

    //設置頁面范圍 打印文字
//    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"哈護手霜按時哈哈"];

//    textFormatter.startPage = 0;//指定從哪一張開始打印0代表第一張

//    textFormatter.contentInsets = UIEdgeInsetsMake(36, 36, 36, 36);//72相當於1英寸,這樣設置上下左右的邊距都為0.5英寸

//    textFormatter.maximumContentWidth = 504;//(72x7.5)相當於打印寬度為7英寸

//    printer.printFormatter = textFormatter;

/*

  • AirPrint可以直接打印一些內容。 這些內容是 NSData, NSURL, UIImage, and ALAsset 類的實例, 但是這些實例的內容, 或者引用的類型(NSURL)必須是 image 或者pdf.
  • 對於 image來說, NSData, NSURL, UIImage, and ALAsset 類型都可以的。 對於PDF, 只能使用 NSData, NSURL。 然后需要將這些數據實例直接賦值 給 UIPrintInteractionController實例的 printingItem 或者 printingItems 屬性。

*/

    // printer.printingItem = [UIImage imageNamed:@"LaunchImage"]; 
    printer.printingItems = @[[UIImage imageNamed:@"welcome_page2"], [UIImage imageNamed:@"LaunchImage"], [UIImage imageNamed:@"welcome_page1"],];

    printer.showsPageRange = NO;

    [printer presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {

        if (!completed && error) {
            NSLog(@"Error");
        }
    }];

}

 

打印webView視圖

初始化webView

- (UIWebView *)printWebView
{
    if (!_printWebView) {

        _printWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height)];

        _printWebView.backgroundColor = HexRGB(0xf9f9f9);

        _printWebView.delegate = self;

        _printWebView.scalesPageToFit = YES;
    }
return _printWebView; }

 

 加載視圖

    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [self.printWebView loadRequest:request];

 

在加載完成中調用打印方法

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [self printWebViewPage];
}

- (void)printWebViewPage{

    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {

        if(error){
            NSLog(@"FAILED! due to error in domain %@ with error code %ld-- %@",

                  error.domain, (long)error.code, completionHandler);
        }
    };

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];

    printInfo.outputType = UIPrintInfoOutputGeneral;

    printInfo.jobName = @"Print for xiaodui";;

    printInfo.duplex = UIPrintInfoDuplexLongEdge;

    controller.printInfo = printInfo;

    controller.showsPageRange = YES;

   
    UIViewPrintFormatter *viewFormatter = [self.printWebView viewPrintFormatter];

    viewFormatter.startPage = 0;

    controller.printFormatter = viewFormatter;
    
    [controller presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
if (!completed && error) { NSLog(@"Error"); } }]; }

 

 

如圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM