在 iOS 中打印 HTML
我目前正在使用以下代码进行打印。请注意,我有 HTML 打印的要求。
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];
}
生成的结果并没有在所有页面上提供一致的边距。部分问题在于 contentInsets
属性。根据 Apple 的文档:顶部插图仅适用于打印内容的第一页。
我希望输出具有一致的边距,就像 Safari 生成的输出一样。
有建议吗?
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 consistent margins, just like the output produced by Safari.
Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您已经得到了工作答案,但是如果您所做的只是添加空白页眉和页脚来填充每个打印的内容,则
UIPrintPageRenderer
子类 并不是绝对必要的页面 - 只需实例化一个普通UIPrintPageRenderer
并设置headerHeight
和footerHeight
属性,然后将您的UIMarkupTextPrintFormatter
添加到渲染器。我在您的其他问题上发布了我的方法:打印纸张尺寸和内容插图< /a>
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 vanillaUIPrintPageRenderer
and set theheaderHeight
andfooterHeight
properties, then add yourUIMarkupTextPrintFormatter
to the renderer.I posted my method on your other question: Print paper size and content inset
使用
UIPrintPageRenderer
子类和UIMarkupTextPrintFormatter
解决了我的问题。苹果的示例代码有帮助。Using
UIPrintPageRenderer
subclass withUIMarkupTextPrintFormatter
solved my problem. Apple's sample code helped.