在 iOS 中打印 HTML

发布于 2024-11-06 02:42:30 字数 1535 浏览 0 评论 0原文

我目前正在使用以下代码进行打印。请注意,我有 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 生成的输出一样。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.
Sample Output generated by the code above

I want the output to have consistent margins, just like the output produced by Safari.Print output produced by Safari

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-11-13 02:42:30

听起来您已经得到了工作答案,但是如果您所做的只是添加空白页眉和页脚来填充每个打印的内容,则 UIPrintPageRenderer 子类 并不是绝对必要的页面 - 只需实例化一个普通 UIPrintPageRenderer 并设置 headerHeightfooterHeight 属性,然后将您的 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 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

蓝天 2024-11-13 02:42:30

使用 UIPrintPageRenderer 子类和 UIMarkupTextPrintFormatter 解决了我的问题。苹果的示例代码有帮助。

Using UIPrintPageRenderer subclass with UIMarkupTextPrintFormatter solved my problem. Apple's sample code helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文