html - 打印 UIWebView 时意外分页

标签 html ios uiwebview pdf-generation uiprintformatter

TLDR:如果您打印包含 HTML 内容的 UIWebView,内容由文本对齐的元素组成/center,生成的文档的页面底部边距会异常大。

我遇到了这个问题,当我无法用谷歌搜索任何有类似问题的人时,我感到非常惊讶。我已经向苹果 (#20760071) 提交了一个雷达,还创建了一个 issueResearchKit 的 GitHub 存储库中,因为这会影响他们的 ORKHTMLPDFWriter

据我所知,这也会影响所有使用 UIWebViewHTML 转换为 PDF 的库,我已经测试过:

我想知道是否有人可以想出一些解决方法。

如何重现:

NSMutableString* html = [NSMutableString string];
[html appendString:@"<html><body>"];
for (int i=0; i<200; i++) {
    [html appendString:@"<div align=\"right\">line</div>"];
}
[html appendString:@"</body></html>"];

UIPrintInteractionController* pc = [UIPrintInteractionController sharedPrintController];

UIPrintInfo* printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGrayscale;

pc.printInfo = printInfo;
pc.showsPaperSelectionForLoadedPapers = YES;

UIWebView* web = [UIWebView new];
[web loadHTMLString:html baseURL:nil];
pc.printFormatter = web.viewPrintFormatter;

[pc presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {
    NSLog(@"%d -- %@",completed, error);

}];

您还可以克隆 projectResearchKit 中演示这个问题。


编辑 - 一些可用的解决方法:

知道内容的特定宽度(例如图像),可以在不指定文本对齐方式的情况下对其进行对齐,例如:

使用自动边距:

<div>
    <div style="width:200px; margin-left:auto; margin-right:0px;">
        content
    </div>
</div>

float 列:

<div>
    <div style="width:200px; float:right;">
        content
    </div>
</div>

但是,以上都不适用于对齐可变长度文本,这是我关注的主要用例。

最佳答案

瓦诺,

经过一些测试后我想出的解决方案是添加以下带有文本对齐的 css 规则。

display: -webkit-box;
display: -webkit-flex;
display: flex;
justify-content: flex-end;
-webkit-justify-content: flex-end;
text-align:right;

关于html - 打印 UIWebView 时意外分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29964523/

相关文章:

javascript - 将 RSS pubdate 字符串转换为 jQuery 中的时间和日期

ios - 从 iPhone 模拟器中删除 Charles SSL 根证书

iphone - 如何创建具有多个SDK版本的iOS应用?

ios - UIWebView 不再工作

iphone - UIWebView 的属性 scalesPageToFit = YES 在较大的图像上无法正常工作

Html5 视频不自动播放

javascript - 是或否单选按钮显示不同的 DIV

ios - 如何在CABasicanimation中设置 anchor

ios - 两个 viewController swift ios 中 uiwebview 的相同实例

javascript - 单击按钮时向文本区域添加值