在iOS中将一个或多个格式化程序与页面呈现程序一起使用

在iOS中将一个或多个格式化程序与页面呈现程序一起使用,ios,printing,uiwebview,uiprintpagerenderer,Ios,Printing,Uiwebview,Uiprintpagerenderer,是否有人尝试过使用多个格式化程序(UIViewPrintFormatter,UIMarkupTextPrintFormatter,UIImpleTextPrintFormatter)和页面呈现程序(UIPrintPageRenderer)来打印内容 我正在尝试将两个UIMarkupTextPrintFormatters与UIPrintPageRenderer子类一起使用,但无法获得打印。我正在使用示例代码中的MyPrintPageRenderer类 我已经看过了苹果的,但是没有什么帮助,也没有与

是否有人尝试过使用多个格式化程序(
UIViewPrintFormatter
UIMarkupTextPrintFormatter
UIImpleTextPrintFormatter
)和页面呈现程序(
UIPrintPageRenderer
)来打印内容

我正在尝试将两个
UIMarkupTextPrintFormatters
UIPrintPageRenderer
子类一起使用,但无法获得打印。我正在使用示例代码中的
MyPrintPageRenderer

我已经看过了苹果的,但是没有什么帮助,也没有与描述相关的示例代码。我尝试过几种解决方案,但到目前为止还没有成功


有什么建议吗?

事实上,“打印”部分的活动很少,这表明使用此API的人不是很多,或者使用此API的人没有访问此社区,或者人们只是出于某种原因忽略了这个问题

无论如何,我能解决我的问题。我之前使用的是
setPrintFormatters:
方法,但该方法不起作用。我不知道为什么。因此,我开始尝试使用
addPrintFormatter:startingAtPageAtIndex:
方法。下面是我解决问题的方法:

// To draw the content of each page, a UIMarkupTextPrintFormatter is used.
NSString *htmlString = [self prepareWebViewHTML];
UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];

NSString *listHtmlString = [self prepareWebViewListHTMLWithCSS];
UIMarkupTextPrintFormatter *listHtmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:listHtmlString];

// I think this should work, but it doesn't! The result is an empty page with just the header and footer text.
// [myRenderer setPrintFormatters:[NSArray arrayWithObjects:htmlFormatter, listHtmlFormatter, nil]];

// Alternatively, i've used addPrintFormatters here, and they just work!
// Note: See MyPrintPageRenderer's numberOfPages method implementation for relavent details.
// The important point to note there is that the startPage property is updated/corrected.
[myRenderer addPrintFormatter:htmlFormatter startingAtPageAtIndex:0];
[myRenderer addPrintFormatter:listHtmlFormatter startingAtPageAtIndex:1];
MyPrintPageRenderer
中,我使用以下代码更新/更正startPage属性,以便为每个格式化程序使用一个新页面:

- (NSInteger)numberOfPages
{
    // TODO: Perform header footer calculations
    // . . .

    NSUInteger startPage = 0;
    for (id f in self.printFormatters) {
        UIPrintFormatter *myFormatter = (UIPrintFormatter *)f;

        // Top inset is only used if we want a different inset for the first page and we don't.
        // The bottom inset is never used by a viewFormatter.
        myFormatter.contentInsets = UIEdgeInsetsMake(0, leftInset, 0, rightInset);

        // Just to be sure, never allow the content to go past our minimum margins for the content area.
        myFormatter.maximumContentWidth = self.paperRect.size.width - 2*MIN_MARGIN;
        myFormatter.maximumContentHeight = self.paperRect.size.height - 2*MIN_MARGIN;

        myFormatter.startPage = startPage;
        startPage = myFormatter.startPage + myFormatter.pageCount;
    }

    // Let the superclass calculate the total number of pages
    return [super numberOfPages];
}
我仍然不知道是否有附加htmlFormatter和listHtmlFormatter的可打印内容的方法(使用这种方法)。e、 g.不要使用列表htmlFormatter的新页面,而是从htmlFormatter结束的地方继续打印。

我在这里添加了这个额外的信息答案,因为我花了两天的时间处理这个问题,而Mustafa的答案救了我。希望所有这些都能在一个地方为其他人节省大量浪费的时间

我试图弄明白为什么我无法让我的自定义
UIPrintPageRenderer
迭代来自多个
uiwebview
viewPrintFormatter
数组,我需要在一个作业中打印,并让它正确计算页面计数,正如这些苹果文档所建议的:

关键是通过这种方法添加它们:

-(void)addPrintFormatter:(UIPrintFormatter *)formatter startingAtPageAtIndex:(NSInteger)pageIndex
而不是这个:

@property(nonatomic, copy) NSArray <UIPrintFormatter *> *printFormatters

要补充@Mustafa的答案, 起始页需要声明为:

__block NSUInteger startPage

如果要在dispatch_async块中使用,请不要,我没有遇到您提到的打印问题。您可以使用上面的代码(使用UIPrintPageRenderer)获得所需的结果。为纯文本创建UISimpleTextPrintFormatter对象,同样为图像创建格式化程序,并使用页面渲染器打印所有图像。或者,您可以使用纯文本和图像创建html文件,并使用UIMarkupTextPrintFormatter和页面渲染器进行打印。如果有帮助的话,我可以和你分享。我使用了图片的原始尺寸。我不理解你的图像大小问题。您是指UIImage的scale属性吗?如何构建html字符串?image.size应返回图像大小,前提是它包含有效的图像引用。大小可以不同于您预期的大小,但仍然应该是有效的CGSize。如果你能发布一段不适合你的代码,我可能会帮你。你不需要用标签来指定图像的高度和宽度。它可以使用默认的图像大小,但如果要使用可调整大小的图像,可以尝试创建HTML,将调整大小的UIImage表示为base64编码的NSData(而不是“file://...“使用[NSString stringWithFormat:@”数据:图像/jpeg;base64,%@,[imageData base64EncodedString]])。e、 是的,我正在使用它,但是在我的例子中,页眉和页脚中的内容不是基于html的。您可以使用苹果的一个示例代码来检查如何添加页眉和页脚。
__block NSUInteger startPage