使用iOS 4.2中的新打印内容生成PDF

gle*_*enc 9 iphone pdf-generation airprint ios-4.2

从历史上看,我的应用程序已将确认生成为纯HTML,并将该HTML传递给普通的MFMailComposeViewController,以便通过电子邮件发送给客户.我想尝试利用iOS 4.2中的新打印类将HTML呈现为PDF并将其作为附件发送.

我尝试了以下方法:

NSString *html = /* generate my HTML here */
NSMutableData *pdfData = [NSMutableData data];
UIMarkupTextPrintFormatter *fmt = [[UIMarkupTextPrintFormatter alloc] 
                                   initWithMarkupText:html];

// Render the html into a PDF
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );

for (NSInteger i=0; i < [fmt pageCount]; i++)
{
    UIGraphicsBeginPDFPage();
    CGRect bounds = UIGraphicsGetPDFContextBounds();
    [fmt drawInRect:bounds forPageAtIndex:i];
}

UIGraphicsEndPDFContext();
Run Code Online (Sandbox Code Playgroud)

问题是[fmt pageCount]始终返回零,因此不会将实际页面内容呈现到PDF NSData中.

有没有人在实际打印作业之外使用UIMarkupTextPrintFormatter将HTML转换为PDF?任何帮助非常感谢.

Jac*_*nce 4

似乎打印格式化程序(包括 UIMarkupTextPrintFormatter)直到打印之前(一旦系统接管并开始打印作业)才真正渲染/绘制。(Apple 的文档说,drawRect: 在打印之前调用,为打印作业提供内容。)

请有人证明我错了,因为我需要做基本上相同的事情:)