Enable printing current webview (#196)

Sometimes when doing complex rendering you want to print the current
webview rather than another page or link. This enables you to call
.print('') and it will print the current webview rather than a blank
page on both ios and android.
This commit is contained in:
Mark Zealey
2018-12-21 14:13:01 +03:00
committed by Sebastián Katzer
parent 675b3f5962
commit 6e44eb9cde
2 changed files with 77 additions and 50 deletions

View File

@@ -382,29 +382,39 @@
*/
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
{
UIWebView* page = [[UIWebView alloc] init];
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
UIViewPrintFormatter* formatter = [page viewPrintFormatter];
UIViewPrintFormatter* formatter;
if([content length] == 0) {
formatter = [self.webView viewPrintFormatter];
} else {
UIWebView* page = [[UIWebView alloc] init];
formatter = [page viewPrintFormatter];
page.delegate = self;
if([content length] == 0) {
// do nothing, already loaded
} else if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
} else {
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
}
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
page.delegate = self;
if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
}
else {
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
controller.printPageRenderer = renderer;
// just trigger the finish load fn straight off if using current webView
if([content length] == 0)
[self webViewDidFinishLoad:(UIWebView *)self.webView];
}
/**