1

Got the following error:

CGDataConsumerCreateWithFilename: failed to open `/name' for writing: Permission denied.

With this code (not mine):

- (void)createPDFWithName
{
//NSURL * newFilePath = [[NSURL alloc] initFileURLWithPath:name];
NSString * newFilePath = @"name";

CGRect page = CGRectMake(0, 0, 300, 300);

NSDictionary * metaData = nil;

if (!UIGraphicsBeginPDFContextToFile(newFilePath, page, metaData )) {
    NSLog(@"error creating PDF context");
    return;
}

UIGraphicsBeginPDFPage();
[self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndPDFContext();
}

So: Where should I store this PDF-File? I tried the NSBundle MainBundle thing and it did not work either.

Thank you!

DAS
  • 1,941
  • 2
  • 27
  • 38

2 Answers2

3

You should read about the various places on the file system that you can create files. One of these is the documents path, an example is below.

- (NSString *)documentPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return documentPath;
}

NSString * newFilePath = [[self documentPath] stringByAppendingPathComponent:@"name.pdf"];
Alex Deem
  • 4,717
  • 1
  • 21
  • 24
  • Thanks for your answer. It does not work as a class method for me? Doing `- (NSString *) ...` works like a charme. – DAS Dec 14 '11 at 11:03
  • oops, shouldn't be a class method sorry. (I copied the code from one of my libs where it *is* a class method) – Alex Deem Dec 14 '11 at 23:58
  • I need help on this.. http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios. could you please suggest – Navnath Memane May 20 '13 at 13:08
2

Expanding on Alex's answer

The documents directory is the ONLY place you should be thinking of storing your PDF. It is the only document store that Apple guarentees persistence of things your app creates and will persist between upgrades etc.

You can't push stuff into your MainBundle as the bundle stays the same throughout the life of your app

Damo
  • 12,840
  • 3
  • 51
  • 62