Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

iphone - Generating a PDF using the new printing stuff in iOS 4.2

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...