tags:

views:

441

answers:

2

Hi,

In my iPhone application, I have several data items.

I want to generate a PDF file using these data items and attach the PDF file in email.

As I know, I need to use Quartz 2D to draw the PDF file.

Is there any sample code or suggestion about drawing PDF?

Thanks in advance.

+1  A: 

This Apple Guide has a code example of just what you want.

TechZen
A: 

The following is what I've used to generate an NSData object containing a PDF representation of some Quartz graphics:

NSMutableData *pdfData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData);

const CGRect mediaBox = CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &mediaBox, NULL);

UIGraphicsPushContext(pdfContext);

CGContextBeginPage(pdfContext, &mediaBox);

// Your Quartz drawing code goes here 

CGContextEndPage(pdfContext); 
CGPDFContextClose(pdfContext);

UIGraphicsPopContext();

CGContextRelease(pdfContext);
CGDataConsumerRelease(dataConsumer);

You can then save this PDF data to disk or add it as an attachment to your email message, remembering to release it when done.

Brad Larson