打印UIWebView时出现意外的分页符

时间:2015-04-30 09:54:51

标签: html ios uiwebview pdf-generation uiprintformatter

TLDR:如果您打印的UIWebView包含HTML内容,其中包含文字对齐right / center的元素,则生成的文档将包含具有意外大底边距的页面。

我遇到了这个问题,当我无法谷歌任何有类似问题的人时,我感到非常惊讶。我已经提交了一个带有苹果的雷达(#20760071)并在ResearchKit的GitHub回购中创建了issue,因为这会影响他们的ORKHTMLPDFWriter

AFAIK这也会影响使用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);

}];

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

<小时/> 编辑 - 有些可用的解决方法:

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

使用自动边距:

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

浮动列:

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

但是,以上都没有用于对齐变长文本,这是我关注的主要用例。

3 个答案:

答案 0 :(得分:3)

瓦诺,

我在一些测试后提出的解决方案是添加以下css规则和文本对齐。

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

答案 1 :(得分:0)

我的text-align解决方案:正确

<header>
    <style type="text/css">
        .default-text-align-right {
            text-align: right;
        }
        .alternative-text-align-right {
            position: absolute;
            right: 10px;
        }
    </style> 
</header>
<body>
    <div>
        <div class="default-text-align-right">
            Default Text Align Left
        </div>
        <div>
            <div class="alternative-text-align-right">Alternative Text Align Right</div>
            <div>&nbsp;</div>
        </div>
        <div class="default-text-align-right">
            Default Text Align Left
        </div>  
    </div>      
</body>

答案 2 :(得分:-1)

我认为这不会对齐可变长度文本,因为文本的长度显然会不断变化,而您的变通方法的div属性具有静态{{1属性。

这是替换字符串的工作,用纸张的大小替换分隔符width(以允许边距填充文本)。要获得此宽度,请调用:

width

但是,为了替换它,您需要在那里设置一个代理,以便在作业开始之前修复HTML代码

让您的委托成为来自pc.printPaper.printRect.size.width; 类型的新类对象作为Xcode的文件创建器中的子类,然后让您的函数触发打印机并在视图中呈现与此类似的内容:

NSObject

(注意:由于某种原因,我在HTML文件中制作了临时内容。我还没有完成!)

在我之前要求你做的委托课中,将此设为NSMutableString* html = [NSMutableString string]; [html appendString:@"<html><body>"]; for (int i=0; i<200; i++) { [html appendString:@"temporary content... "]; } [html appendString:@"</body></html>"]; UIWebView* web = [UIWebView new]; [web loadHTMLString:html baseURL:nil]; UIPrintInteractionController* pc = [UIPrintInteractionController sharedPrintController]; pc.printFormatter = web.viewPrintFormatter; UIPrintInfo* printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGrayscale; printerDelegate* pd = [printerDelegate new]; pc.printInfo = printInfo; pc.showsPaperSelectionForLoadedPapers = YES; pc.delegate = pd; [pc presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { NSLog(@"%d -- %@",completed, error); }]; 文件

.h

然后,在它的 #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface printerDelegate : NSObject <UIPrintInteractionControllerDelegate> @end 文件中,我有这个......

.m

它返回一个错误的访问异常,但是你得到了要点:在用户选择他们的纸张类型后,将分隔符#import "printerDelegate.h" #import <UIKit/UIKit.h> @implementation printerDelegate -(void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController { @try { NSMutableString* html = [NSMutableString string]; [html appendString:@"<html><body>"]; for (int i=0; i<200; i++) { [html appendString:[NSString stringWithFormat:@"<div><div style=""width:%fpx; margin-left:auto; margin-right:0px;"">content</div></div>", printInteractionController.printPaper.printRect.size.width]]; } [html appendString:@"</body></html>"]; UIWebView* web = [UIWebView new]; [web loadHTMLString:html baseURL:nil]; printInteractionController.printFormatter = web.viewPrintFormatter; } @catch (NSException *exception) { NSLog(@"%@", exception.debugDescription); } @finally { } } @end 设置为打印区域的宽度。唯一的另一种选择是要么具有指定的宽度(就像您在编辑中建议的那样),要么使用一种特定类型的纸张供用户打印。

发生这种情况的原因是因为打印width的HTML时出错,而不是因为编码。希望Apple在以后的更新中修复此问题。

相关问题