Creating a simple pdf
I'm trying to create a PDF on the iPhone for practice. I'm not sure why it doesn't work. I get the error:
2011-09-21 21:35:00.412 CreatePDF[869:b303] -[CreatePDFViewController createPDFDocumentAtURL:]
CreatePDF[869] <Error>: CGDataConsumerCreateWithFilename: failed to open `/Users/jon/Library/Application Support/iPhone Si开发者_开发知识库mulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents' for writing: Is a directory.
deflateEnd: error -3: (null).
CreatePDF[869] <Error>: CGPDFContextCreate: failed to create PDF context delegate.
2011-09-21 21:35:00.419 CreatePDF[869:b303] Couldn't create PDF context
<Error>: CGContextBeginPage: invalid context 0x0
Here is the code I have:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *documentsURL = [NSURL fileURLWithPath:documentsDirectory];
[self createPDFDocumentAtURL:(CFURLRef)documentsURL];
NSLog(@"%s", __FUNCTION__);
}
- (void)createPDFDocumentAtURL:(CFURLRef)url {
NSLog(@"%s", __FUNCTION__);
float red[] = {1., 0., 0., 1. };
CGRect mediaBox = CGRectMake(0, 0, 850, 1100);
CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &mediaBox, NULL);
if (!pdfContext) {
NSLog(@"Couldn't create PDF context");
}
CGContextBeginPage(pdfContext, &mediaBox);
CGContextSaveGState(pdfContext);
CGContextClipToRect(pdfContext, mediaBox);
CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateDeviceRGB());
CGContextSetFillColor(pdfContext, red);
CGContextFillRect(pdfContext, mediaBox);
CGContextRestoreGState(pdfContext);
CGContextEndPage(pdfContext);
CGContextRelease(pdfContext);
}
You have to add the new file's name to your URL. Your documentsURL contains this right now:
/Users/jon/Library/Application Support/iPhone Simulator/4.3.2/Applications/4486CF27-B309-4C7C-82A5-B425B9E2C04D/Documents/
精彩评论