TLDR:如果您打印包含 HTML
内容的 UIWebView
,内容由文本右
对齐的元素组成/center
,生成的文档的页面底部边距会异常大。
我遇到了这个问题,当我无法用谷歌搜索任何有类似问题的人时,我感到非常惊讶。我已经向苹果 (#20760071) 提交了一个雷达,还创建了一个 issue在 ResearchKit
的 GitHub 存储库中,因为这会影响他们的 ORKHTMLPDFWriter
。
据我所知,这也会影响所有使用 UIWebView
将 HTML
转换为 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);
}];
您还可以克隆 project在 ResearchKit
中演示这个问题。
编辑 - 一些可用的解决方法:
知道内容的特定宽度(例如图像),可以在不指定文本对齐方式的情况下对其进行对齐,例如:
使用自动边距:
<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/