Created
July 17, 2013 16:39
-
-
Save katsuhide/6022267 to your computer and use it in GitHub Desktop.
WebKit周り
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
| // Webページを開く | |
| - (IBAction)loadWebPage:(id)sender | |
| { | |
| NSLog(@"load web page"); | |
| NSString *urlAddress = [_urlField stringValue]; | |
| NSURL *url = [NSURL URLWithString:urlAddress]; | |
| NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; | |
| [[_webView mainFrame] loadRequest:requestObj]; | |
| } | |
| // WebViewをpdfとして保存 | |
| -(void)savePDFbyNormal{ | |
| NSView* view = [[[_webView mainFrame] frameView] documentView]; | |
| NSRect rect = [view bounds]; | |
| NSData* outdata = [view dataWithPDFInsideRect:rect]; | |
| NSString* path = @"/Users/Desktop/normal.pdf"; | |
| [outdata writeToFile:path atomically:YES]; | |
| } | |
| // WebViewを印刷ダイアログを使ってpdfとして保存 | |
| -(void)savePDFbyPrinter{ | |
| // (1) setup filename | |
| NSString* path = @"/Users/Desktop/printer.pdf"; | |
| // (2) setup WebView | |
| NSView* view = [[[_webView mainFrame] frameView] documentView]; | |
| // (3) setup NSDictionary for NSPrintInfo | |
| NSMutableDictionary* p_dict = [NSMutableDictionary | |
| dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]]; | |
| [p_dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition]; | |
| [p_dict setObject:path forKey:NSPrintSavePath]; | |
| // (4) setup NSPrintInfo | |
| NSPrintInfo* p_info = [[NSPrintInfo alloc] initWithDictionary:p_dict]; | |
| [p_info setHorizontalPagination:NSAutoPagination]; | |
| [p_info setVerticalPagination:NSAutoPagination]; | |
| [p_info setVerticallyCentered:NO]; | |
| // (5) setup NSPrintOperation | |
| NSPrintOperation* p_ope = [NSPrintOperation printOperationWithView:view printInfo:p_info]; | |
| [p_ope setShowPanels:NO]; | |
| // (6) print it | |
| [p_ope runOperation]; | |
| } | |
| // 画像として保存 | |
| -(void)saveImage{ | |
| NSBitmapImageRep* bitmap = | |
| [_webView bitmapImageRepForCachingDisplayInRect:[_webView bounds]]; | |
| [_webView cacheDisplayInRect:[_webView bounds] | |
| toBitmapImageRep:bitmap]; | |
| NSData* outdata = [bitmap representationUsingType:NSPNGFileType | |
| properties:[NSDictionary dictionary]]; | |
| NSArray *paths = NSSearchPathForDirectoriesInDomains( | |
| NSDesktopDirectory, NSUserDomainMask, YES); | |
| NSString* path = @"/Users/Desktop/image.png"; | |
| [outdata writeToFile:path atomically:YES]; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment