使用 iOS 4.2 中的新打印功能生成 PDF

发布于 2024-10-06 12:33:10 字数 865 浏览 0 评论 0原文

从历史上看,我的应用程序生成了纯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难得心□动 2024-10-13 12:33:10

似乎打印格式化程序(包括 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 :)

如若梦似彩虹 2024-10-13 12:33:10

我在我的一个应用程序中执行此操作。请参阅我对类似问题的回答,此处:

有没有办法在 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

涫野音 2024-10-13 12:33:10

我发现:如果您愿意或被允许使用私有 API,这是可能的。 (例如,在企业应用程序中。)

方法:
像往常一样创建格式化程序;将其安装在 UIPrintPageRenderer 中。

适当设置渲染器的私有属性paperRectprintableRect

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 and printableRect 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文