views:

68

answers:

1

I want to have my iPhone application create a PDF file and store it in an appropriate place - probably the iBooks folder so it can be easily retrieved using iTunes, but that's a change for later once I have the saving part working. I've followed the PDF saving tutorial on the Apple site to the letter, including swapping out CGContextBeginPage and CGContextEndPage for their PDF specific equivalents, and I've checked that the save filename is set to the correct spot, but either no file is being created or I just can't find where it's being stored. Here's the code, any ideas what's wrong? Is it the code, or does this kind of operation just not run in the simulator?

    CGRect b = CGRectMake(0, 0, 612, 792);
NSMutableString *exportPath = [[NSMutableString alloc] 
                               initWithString:[[NSBundle mainBundle] bundlePath]];
[exportPath appendString:@"/myfile.pdf"];
CGContextRef pdfContext;
CFStringRef path = CFStringCreateWithCString (NULL, [exportPath UTF8String], kCFStringEncodingUTF8);
CFURLRef url; CFMutableDictionaryRef myDictionary = NULL;

url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease(path);
myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

pdfContext = CGPDFContextCreateWithURL (url, &b, myDictionary);
CFRelease(myDictionary);
CFRelease(url);

CGPDFContextBeginPage(pdfContext, NULL);
[self drawToContext: pdfContext Bounds:b];
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CGContextRelease (pdfContext);  
A: 

You are trying to save the PDF into your bundle. I'm pretty sure you can do that. Use the document path instead.

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbpath = [documentsDir stringByAppendingPathComponent:@"myfile.pdf"];
Marcelo Cantos
Thanks, that seems to have sorted it out. Mind you, I now seem to have a slightly different problem in that some of the graphical commands I'm using aren't PDF compliant... oh well! At least I'm moving again.
Ash