如何一次性AirPrint多个打印格式化程序?

Swe*_*per 16 ios airprint swift

我的最终目标是创建多个UIMarkupTextPrintFormatter并在一个打印作业中打印它们.

例如,第一个格式化程序包含填充第一页和第二页的一小部分的文本.虽然还有空间可以打印第二个格式化程序,但我希望第二个格式化程序在第三页上打印.基本上,在打印另一个格式化程序之前,请先启动一个新页面.

我不知道一个格式化程序在编译时会填满多少页面,因为它取决于用户输入.也许1,2或更多.

我听说我需要使用,UIPrintPageRenderer所以我阅读了它的文档并尝试了这个:

// I'm using webviews here because I don't want to write 2 pages worth of markup...
// it should be the same anyway. The two webviews display different wikipedia articles
// Also, don't judge my variable names. I'm lazy and this is just test code...
let formatter = webView.viewPrintFormatter()
let formatter1 = webView2.viewPrintFormatter()

formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
formatter1.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)

let renderer = UIPrintPageRenderer()
renderer.addPrintFormatter(formatter, startingAtPageAt: 0)
renderer.addPrintFormatter(formatter1, startingAtPageAt: formatter.pageCount)

printController.printPageRenderer = renderer
printController.present(animated: true, completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

为了添加第二个打印格式化程序,我使用formatter.pageCount了第二个参数.我希望它会在第一页的最后一页之后添加第二个格式化程序.但事实并非如此.它只打印第二格式化程序中的内容.

我打印formatter.pageCount并发现它的值为0.

我完全糊涂了.如何获取格式化程序将填充的页数?

我还尝试实现自己的渲染器:

class MyRenderer: UIPrintPageRenderer {
    override func drawPrintFormatter(_ printFormatter: UIPrintFormatter, forPageAt pageIndex: Int) {
        printFormatter.draw(in: self.printableRect, forPageAt: pageIndex)
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当打印交互控制器显示时它只会崩溃:

试图从主线程或Web线程以外的线程获取Web锁.这可能是从辅助线程调用UIKit的结果.现在崩溃......

我的代码中出了什么问题?

man*_*mku 2

根据文档,UIPrintFormatter 在计算以格式打印的页数之前需要所有信息。

UIPrintFormatter 发布了一个接口,允许您指定打印作业的起始页以及打印内容周围的边距;给定该信息加上内容,打印格式化程序会计算打印作业的页数。

因此,除非 UIPrintFormatter 开始打印内容,否则不会发生这种情况。

由于UIPrintPageRenderer可能使用UIPrintFormatter它实际上可以获取与格式化程序关联的页面数,您所要做的就是覆盖 numberOfPages 以在每个页面设置不同的格式化程序。

以下是 Apple 的示例代码。它做了完全相同的事情 - 在每个页面上附加多个格式化程序(UIMarkupTextPrintFormatter)。

它在这里覆盖 numberOfPages:

- (NSInteger)numberOfPages {
    self.printFormatters = nil;
    [self setupPrintFormatters];
    return [super numberOfPages];
}
Run Code Online (Sandbox Code Playgroud)

并将格式化程序附加到此处的每个页面:

  /*
     Iterate through the recipes setting each of their html representations into 
     a UIMarkupTextPrintFormatter and add that formatter to the printing job.
     */
    - (void)setupPrintFormatters {
        NSInteger page = 0;
        CGFloat previousFormatterMaxY = CGRectGetMinY(self.contentArea);

        for (Recipe *recipe in recipes) {
            NSString *html = recipe.htmlRepresentation;

            UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
            [formatterToRecipeMap setObject:recipe forKey:formatter];

            // Make room for the recipe info
            UIEdgeInsets contentInsets = UIEdgeInsetsZero;
            contentInsets.top = previousFormatterMaxY + self.recipeInfoHeight;
            if (contentInsets.top > CGRectGetMaxY(self.contentArea)) {
                // Move to the next page
                page++;
                contentInsets.top = CGRectGetMinY(self.contentArea) + self.recipeInfoHeight;
            }
            formatter.contentInsets = contentInsets;

            // Add the formatter to the renderer at the specified page
            [self addPrintFormatter:formatter startingAtPageAtIndex:page];

            page = formatter.startPage + formatter.pageCount - 1;
            previousFormatterMaxY = CGRectGetMaxY([formatter rectForPageAtIndex:page]);

        }
    }
Run Code Online (Sandbox Code Playgroud)

简而言之,它是根据内容区域、内容插入计算页码,然后通过调用 API 将格式化程序附加到页面:

 [self addPrintFormatter:formatter startingAtPageAtIndex:page];
Run Code Online (Sandbox Code Playgroud)

这样渲染器就有了每个页面格式化程序的信息。您可以在这里找到完整的代码

希望它会有所帮助。