關於ios打印(AirPrint)


實例1:

        Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
        
        if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
        {
            printInteraction = [printInteractionController sharedPrintController];
            printInteraction.delegate = self;
            
            UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];
            
            printInfo.duplex = UIPrintInfoDuplexLongEdge;
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
            
            printInteraction.printInfo = printInfo;
            printInteraction.showsPageRange = YES;
            
            UIViewPrintFormatter *formatter = [webView viewPrintFormatter];
            printInteraction.printFormatter = formatter;
            
            [printInteraction presentFromBarButtonItem:actionButtonItem
                                              animated:YES
                                     completionHandler:
             ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
             }
             ];
        }

實例2:

    Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");

    if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
    {
        NSURL *fileURL = document.fileURL; // Document file URL

        printInteraction = [printInteractionController sharedPrintController];

        if ([printInteractionController canPrintURL:fileURL] == YES) // Check first
        {
            UIPrintInfo *printInfo = [NSClassFromString(@"UIPrintInfo") printInfo];

            printInfo.duplex = UIPrintInfoDuplexLongEdge;
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = document.fileName;

            printInteraction.printInfo = printInfo;
            printInteraction.printingItem = fileURL;
            printInteraction.showsPageRange = YES;

            if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
            {
                [printInteraction presentFromRect:button.bounds inView:button animated:YES completionHandler:
                    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                    {
                        #ifdef DEBUG
                            if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
                        #endif
                    }
                ];
            }
            else // Presume UIUserInterfaceIdiomPhone
            {
                [printInteraction presentAnimated:YES completionHandler:
                    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                    {
                        #ifdef DEBUG
                            if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
                        #endif
                    }
                ];
            }
        }
    }

利用UIPrintPageRenderer將html轉換成pdf   

Boss讓我調查一下把html轉換成PDF的方法,google之,有很多,都是用webview加載,截屏,轉換。
於是去向Boss報告,結果他說這樣轉換成的pdf是純圖片的,不能對文本進行操作了,達不到想要的效果。
無奈,繼續google。結果,再一次領略了stackoverflow的強大,最終找到一個解決方案:http://stackoverflow.com/q/9528658/966127
實現過程,子類化UIPrintPageRenderer

 

- (CGRect) paperRect
{
    if (!_generatingPdf)
        return [super paperRect];

    return UIGraphicsGetPDFContextBounds();
}

- (CGRect) printableRect
{
    if (!_generatingPdf)
        return [super printableRect];

    return CGRectInset( self.paperRect, 20, 20 );
}

- (NSData*) printToPDF
{
    _generatingPdf = YES;

    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData( pdfData, CGRectMake(0, 0, 792, 612), nil );  // letter-size, landscape

    [self prepareForDrawingPages: NSMakeRange(0, 1)];

    CGRect bounds = UIGraphicsGetPDFContextBounds();

    for ( int i = 0 ; i < self.numberOfPages ; i++ )
    {
        UIGraphicsBeginPDFPage();

        [self drawPageAtIndex: i inRect: bounds];
    }

    UIGraphicsEndPDFContext();

    _generatingPdf = NO;

//    NSString* filename = @"/Volumes/Macintosh HD 2/test.pdf";
//    [pdfData writeToFile: filename  atomically: YES];

    return pdfData;
}
UIViewPrintFormatter *viewFormatter = [htmlWebView viewPrintFormatter];
    [ppRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
    NSData *pdfData = [ppRenderer printToPDF];
    NSString *pdfPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.pdf"];
    [pdfData writeToFile:pdfPath atomically:YES];


免責聲明!

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



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