Skip to content

Instantly share code, notes, and snippets.

@niwatako
Created June 16, 2016 05:28
Show Gist options
  • Save niwatako/e7b87973284636a3ceedda5904a84c44 to your computer and use it in GitHub Desktop.
Save niwatako/e7b87973284636a3ceedda5904a84c44 to your computer and use it in GitHub Desktop.
@energy_gorilla WKWebViewのPDFプリント、これでどうでしょう #CodePiece
- (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