PDA

View Full Version : UIWebview loadHTMLString cutting off pages on airprint




Wunk
Sep 10, 2012, 03:00 PM
I'm trying to print from a UIWebView. Problem is, that the original UIWebView has white text on a transparent background resulting in a white print. So I'm doing some html rewriting by loading the contents of the original UIWebView into an NSString, do a few stringByReplacingOccurrencesOfString to alter colors and add in a logo at top and load the result into a freshly allocated UIWebView and call viewPrintFormatter on that.

Now this works fine, UNTIL it spans > 1 page, the printController simply will not show the option to print multiple pages when I allocate a new UIWebView and load a string into that. It simply cuts the content off at the end of the first page. (all the HTML is there, verified by the NSLog entry)

Loading the original webview into the print controller works fine and shows multiple page print options..

This is the code I use (jsonReceptHTML is the original UIWebView with the existing content):


-(IBAction)printRecept:(id)sender {
NSString *printHTMLcodeOriginal = [NSString stringWithFormat:@"%@", [jsonReceptHTML stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].outerHTML"]];
NSString *printHTMLcodeNoLogo = [printHTMLcodeOriginal stringByReplacingOccurrencesOfString:@"white" withString:@"black"];
NSString *printHTMLcodeAddFooter = [printHTMLcodeNoLogo stringByReplacingOccurrencesOfString:@"</body>" withString:@"<br><br><hr><br><center><strong>Some footer banner</strong></center></body>"];
NSString *printHTMLcode = [printHTMLcodeAddFooter stringByReplacingOccurrencesOfString:@"</head><body>" withString:@"</head><body><center><img src='http://www.somesite.com/images/logo.png'></center>"];


NSLog(@"HTML code:%@",printHTMLcode);
UIWebView *printWebView = [[UIWebView alloc] init];
[printWebView loadHTMLString:printHTMLcode baseURL:nil];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInformation = [UIPrintInfo printInfo];
printInformation.outputType = UIPrintInfoOutputGeneral;
pic.showsPageRange = YES;
pic.printInfo = printInformation;
pic.printFormatter = [printWebView viewPrintFormatter];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
{
if (!completed && error)
{
NSLog(@"Kan niet printen, fout: %@", error);
}
};
[pic presentAnimated:YES completionHandler:completionHandler];
[printWebView release];
}


I must be missing something here. I've also tried not using the stringByEvaluatingJavaScriptFromString stripping function but instead initiating a new NSURL request to grab the HTML (expensive I know), but that gives the same result..

Anyone got a clue what I might be overlooking here ?



Wunk
Sep 11, 2012, 02:56 PM
Found the reason.., since UIWebView loads asynchronously, it would render the webview to the printer controller before it would finish loading, giving an incomplete page.

Solved it by adding a check in the webViewDidFinish load to see if the print function was called through setting a BOOL value, and if so call the UIPrinterActionController :-)