html – 打印UIWebView时意外的分页符

前端之家收集整理的这篇文章主要介绍了html – 打印UIWebView时意外的分页符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
TLDR:如果您打印包含 HTML文本的UIWebView,该内容包含文本对齐/中心的元素,则生成的文档将具有意外大的底部页边距的页面.

我遇到这个问题,当我不能google任何有类似问题的人时,非常惊讶.我已经向苹果提出了一个雷达(#20760071),并在ResearchKit的GitHub repo上创建了一个issue,因为这影响了他们的ORKHTMLPDFWriter.

AFAIK这也影响所有使用UIWebView将HTML转换为PDF的库,我已经测试过:

> BNHtmlPdfKit
> NDHTMLtoPDF

我想知道有没有人可以想出一些解决方法.

如何复制:

@H_502_13@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); }];

您还可以克隆在ResearchKit中演示此问题的project.

编辑 – 有些可用的解决方法

知道内容的特定宽度(例如图像),可以对齐它而不指定文本对齐,例如:

使用自动边距:

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

浮动列:

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

但是,上面没有一个用于对齐可变长度的文本,这是我关心的主要用例.

解决方法

瓦诺,

在一些测试之后我想出的解决方案是添加以下css规则与文本对齐.

@H_502_13@display: -webkit-Box; display: -webkit-flex; display: flex; justify-content: flex-end; -webkit-justify-content: flex-end; text-align:right;

猜你在找的HTML相关文章