Created
June 16, 2016 05:28
-
-
Save niwatako/e7b87973284636a3ceedda5904a84c44 to your computer and use it in GitHub Desktop.
@energy_gorilla WKWebViewのPDFプリント、これでどうでしょう #CodePiece
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)print { | |
// self.wkWebView はプロジェクト内でのWKWebViewインスタンスに差し替えてください。 | |
// PrintInfo を作る | |
UIPrintInfo *printInfo = [UIPrintInfo printInfo]; | |
printInfo.outputType = UIPrintInfoOutputGeneral; | |
printInfo.jobName = self.wkWebView.title; | |
printInfo.orientation = UIPrintInfoOrientationPortrait; | |
printInfo.duplex = UIPrintInfoDuplexLongEdge; | |
// PrintInteractionController を作る | |
UIPrintInteractionController *printInteractionController = [UIPrintInteractionController sharedPrintController]; | |
printInteractionController.printInfo = printInfo; | |
printInteractionController.showsPageRange = YES; | |
// PDFデータの取得を試みる | |
NSData *PDFData; | |
if ([self.wkWebView respondsToSelector:@selector(_dataForDisplayedPDF)]) { | |
PDFData = (NSData*)[self.wkWebView performSelector:@selector(_dataForDisplayedPDF)]; | |
} | |
if (PDFData){ | |
// PDFが取れていればPrintInteractionControllerにItemとしてPDFデータをセット | |
printInteractionController.printingItem = PDFData; | |
} else { | |
// PDFがなければPrintInteractionControllerにWKWebViewのprintFormatterをセット | |
printInteractionController.printFormatter = self.wkWebView.viewPrintFormatter; | |
} | |
//印刷画面を表示する | |
[printInteractionController presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { | |
if (error) { | |
NSLog(@"%@",error.description); | |
return; | |
} | |
NSLog(@"complete"); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment