使用 iOS 4.2 中的新打印功能生成 PDF
从历史上看,我的应用程序生成了纯 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();
问题是 [fmt pageCount]
始终返回零,因此没有实际页面内容渲染到 PDF NSData 中。
有没有人在实际打印作业之外使用 UIMarkupTextPrintFormatter 将 HTML 转换为 PDF 时运气好?非常感谢任何帮助。
Historically, my app has generated confirmations as plain HTML and passed that HTML to the normal MFMailComposeViewController for emailing to the customer. I wanted to try to leverage the new printing classes in iOS 4.2 to render the HTML to a PDF instead and send that as an attachment.
I tried the following:
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();
The problem is that [fmt pageCount]
always returns zero, so no actual page content is ever rendered into the PDF NSData.
Has anyone had any luck using UIMarkupTextPrintFormatter outside of an actual print job to convert HTML to PDF? Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎打印格式化程序(包括 UIMarkupTextPrintFormatter)直到打印之前(一旦系统接管并开始打印作业)才真正渲染/绘制。 (Apple 的文档说 drawRect: 在打印之前调用,为打印作业提供内容。)
请有人证明我错了,因为我需要做基本上相同的事情:)
It seems as though print formatters (including UIMarkupTextPrintFormatter) aren't actually rendered/drawn until right before printing, once the system takes over and starts the print job. (Apple's docs say drawRect: is called right before printing to provide the content for the print job.)
Someone please prove me wrong because I need to do basically the same thing :)
我在我的一个应用程序中执行此操作。请参阅我对类似问题的回答,此处:
有没有办法在 iOs 中从 XML/HTML 模板生成 PDF 文件
I do this in one of my apps. See my answer to a similar question, here:
Is there any way to generate PDF file from a XML/HTML template in iOs
我发现:如果您愿意或被允许使用私有 API,这是可能的。 (例如,在企业应用程序中。)
方法:
像往常一样创建格式化程序;将其安装在 UIPrintPageRenderer 中。
适当设置渲染器的私有属性
paperRect
和printableRect
。numberOfPages
现在可以使用了!像平常一样设置 pdf cgcontext,并使用渲染器的
drawPageAtIndex:inRect:
方法绘制页面。天啊,它成功了。强制性声明:是的,我知道 Apple 不希望您提交调用(例如)
[ppr setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
的应用程序,并且会退回它们。I made a discovery: this is possible, if you're willing or allowed to use private API. (e.g., in an enterprise app.)
The method:
create your formatter as usual; install it in a UIPrintPageRenderer.
Set the renderer's private properties
paperRect
andprintableRect
appropriately.numberOfPages
now works!Set up your pdf cgcontext like normal, and draw the page with the renderer's
drawPageAtIndex:inRect:
method. Holy cow, it worked.Obligatory protestation: yes, I know Apple doesn't want you submitting apps that call (e.g.)
[ppr setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
and will bounce them.