Printing HTML in iOS
I'm using following code for printing at the moment. Note that i have the requirement of HTML printing.
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample";
pic.printInfo = printInfo;
NSString *htmlString = [self prepareHTMLEmailMessage];
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
htmlFormatter.startPage = 0;
htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1-inch margins on all sides
htmlFormatter.maximumContentWidth = 6 * 72.0; // printed content should be 6-inches wide within those margins
pic.printFormatter = htmlFormatter;
[htmlFormatter release];
pic.showsPageRange = YES;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"Printing could not complete because of error: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:self.myPrintBarButton animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
The result produced doesn't give me consistent margins on all pages. Part of the problem is the contentInsets
property. As per Apple's documentation: The top inset applies only to the first page of printed content.
I want the output to have consi开发者_如何学编程stent margins, just like the output produced by Safari.
Suggestions?
Sounds like you've already got your working answer, but a UIPrintPageRenderer
subclass is not strictly necessary if all you are doing is adding a blank header and footer to pad out each printed page - just instantiate a vanilla UIPrintPageRenderer
and set the headerHeight
and footerHeight
properties, then add your UIMarkupTextPrintFormatter
to the renderer.
I posted my method on your other question: Print paper size and content inset
Using UIPrintPageRenderer
subclass with UIMarkupTextPrintFormatter
solved my problem. Apple's sample code helped.
精彩评论