10

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.

glenc
  • 3,132
  • 2
  • 26
  • 42

3 Answers3

4

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

Community
  • 1
  • 1
TomSwift
  • 39,369
  • 12
  • 121
  • 149
4

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 :)

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
  • Ehhhhhh .... what a bummer. Looks like you can't hijack the print formatters to do in-memory stuff too easily. – glenc Dec 08 '10 at 14:00
  • Sounds like a Radar at Apple is in order :) – Tom Irving Dec 08 '10 at 16:13
  • Agreed. Did you submit a radar yet? If so, what's the radar number? I'll submit as well. – Jack Lawrence Dec 11 '10 at 21:04
  • Actually I didn't submit a Radar ... haven't done that before and got swamped with other things – glenc Dec 19 '10 at 04:10
  • I submitted an Apple Developer Technical Support (DTS) request a few days ago and the guy got back to me and was basically like yeah, this isn't possible, and directed me to submit a feature request. My bug number is 8793893. – Jack Lawrence Dec 21 '10 at 16:16
  • Hi Jack - just wondering if anything ever happened on that Radar bug? Hoping that perhaps we'll see some joy on the HTML-to-PDF front for iOS 5 – glenc Aug 30 '11 at 11:40
  • Hi glenc, the bug report was closed as a duplicate. I'll check in with apple and see what's up. – Jack Lawrence Sep 07 '11 at 23:18
  • Hey mamcx, I checked my bug report and the status is still listed as "open" by Apple, so I'm assuming no. Take a look at the 4.3 -> 5.0 API diff document on developer.apple.com if you want to be sure. – Jack Lawrence Dec 12 '11 at 06:15
1

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.

rgeorge
  • 7,385
  • 2
  • 31
  • 41
  • Thanks for the info - interesting to know how Apple does it, hopefully they'll make this public in a future release at some point ... – glenc Nov 16 '11 at 02:30