Has anyone already created a PDF document in an iPad app? I see that there are new functions in the UIKit to do this, but I can\'t find any code example for it.
<
Here is a method that will create a new PDF document. It does not do text formatting though.
- (void) createNewPDF: (NSString *) saveFileName withContent: (NSString *) content forSize: (int) fontSize andFont:(NSString *) font andColor: (UIColor *) color
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:saveFileName];
CGRect a4Page = CGRectMake(0, 0, 595, 842);
NSDictionary *fileMetaData = [[NSDictionary alloc] init];
if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData )) {
NSLog(@"error creating PDF context");
return;
}
CGContextRef mpdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
CGContextSetTextMatrix(mpdfContext, CGAffineTransformMake(1, 0, 0, -1, 0, 0));
CGContextSetTextDrawingMode (mpdfContext, kCGTextFill);
CGContextSelectFont (mpdfContext, [font cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);
CGContextSetFillColorWithColor(mpdfContext, [color CGColor]);
CGContextShowTextAtPoint (mpdfContext, 20, 20, [content cStringUsingEncoding:NSUTF8StringEncoding], [content length]);
UIGraphicsEndPDFContext();
}