Ios UIMarkupTextPrintFormatter打印额外空白页

Ios UIMarkupTextPrintFormatter打印额外空白页,ios,printing,Ios,Printing,当使用UIMarkupTextPrintFormatter打印几行简单的HTML时,它首先输出一个空白页面,然后输出包含文本的页面。代码如下,非常简单: - (void) printSomething; { if (![UIPrintInteractionController isPrintingAvailable]) return; NSString* markupText =@"<html><body>THIS IS A TEST&l

当使用UIMarkupTextPrintFormatter打印几行简单的HTML时,它首先输出一个空白页面,然后输出包含文本的页面。代码如下,非常简单:

- (void) printSomething;
{
    if (![UIPrintInteractionController isPrintingAvailable])
        return;

    NSString* markupText =@"<html><body>THIS IS A TEST</body></html>";

    UIMarkupTextPrintFormatter* printFormatter =[ [ [UIMarkupTextPrintFormatter alloc] initWithMarkupText:markupText] autorelease];

    UIPrintInteractionController* printInteractionController =[UIPrintInteractionController sharedPrintController];
    printInteractionController.printFormatter =printFormatter;
    printInteractionController.delegate =self;  
    //printInteractionController.showsPageRange =YES;       

    [printInteractionController presentAnimated:YES completionHandler:nil];                                 
}
-(void)打印某物;
{
如果(![UIPrintInteractionController isPrintingAvailable])
返回;
NSString*markupText=@“这是一个测试”;
UIMarkupTextPrintFormatter*printFormatter=[[UIMarkupTextPrintFormatter alloc]initWithMarkupText:markupText]autorelease];
UIPrintInteractionController*printInteractionController=[UIPrintInteractionController sharedPrintController];
printInteractionController.printFormatter=printFormatter;
printInteractionController.delegate=self;
//printInteractionController.showsPageRange=YES;
[printInteractionController presentAnimated:YES completionHandler:nil];
}
现在,如果我取消对showsPageRange=YES的注释,一个页面将按预期打印,但是UIPrintInteractionController需要几秒钟才能显示。足以让用户怀疑应用程序是否冻结

UIMarkupTextPrintFormatter文档的第一行说明“UIMarkupTextPrintFormatter类的实例为多页打印作业布局HTML标记文本”。如果格式化程序打印多个页面,不管内容如何,那就有点疯狂了


知道这里怎么了吗?其他应用程序这样做没有任何问题。提前感谢。

我在第二个空白页出现时遇到了与
printInteractionController.showsPageRange=NO相同的问题
并找到了Apple示例(第67页)。这是:

- (IBAction)printContent:(id)sender {
    UIPrintInteractionController *pic = [UIPrintInteractionController
                                         sharedPrintController];
    pic.delegate = self;
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = self.documentName;
    pic.printInfo = printInfo;
    UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:self.htmlString];
    htmlFormatter.startPage = 0;
    htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
    pic.printFormatter = htmlFormatter;
    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:sender animated:YES
                    completionHandler:completionHandler];
    } else {
        [pic presentAnimated:YES completionHandler:completionHandler];
    }
}
本例使用
printInteractionController.showsPageRange=YES,工作正常,但如果将此行替换为

printInteractionController.showsPageRange=NO,它会额外打印第二个空白页


因此,似乎
UIMarkupTextPrintFormatter
隐含地用于
printeractioncontroller.showsPageRange=YES,或者它只是一个API错误。

我通过使用正确的HTML框架解决了它:

NSString *htmlString = "<!DOCTYPE html><html><head><meta charset='UTF-8'><title>Title</title></head><body>Hello!</body></html>"
NSString*htmlString=“TitleHello!”

我也遇到了同样的问题,我发现它是由html代码引起的

style='page-break-after:always;

正如@Hassy在回答中提到的,问题可能在于html/css代码

在我的例子中,我设置了
html,body{min height:100%;}
,这可能与一些边距/填充不匹配,但问题可能与css有关。

背景色
设置为
主体
将有助于检查这是否确实是问题所在(如果“空白”页面上的颜色重叠,则问题在于样式)。

我已经将
showsPageRange
设置为
。但它仍在打印附加到最后一页的额外空白页。另外,请确保
之后:
标题