开发者

iphone pdf save to document store

开发者 https://www.devze.com 2023-01-30 01:36 出处:网络
I am rendering a pdf by creating a pdf context and giving it a path as defined below: NSFileManager *fileManager = [NSFileManager defaultManager];

I am rendering a pdf by creating a pdf context and giving it a path as defined below:

    NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [docsDirectory stringByAppendingPathComponent:@"mypdf.pdf"];

And then using that path like the following:

    char * fileutf8 = [file UTF8String];
    CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
path = CFStringCreateWithCString (NULL, fileutf8,kCFStringEncodingUTF8);
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 = CGPDFContextCreateW开发者_开发技巧ithURL (url, &size, myDictionary);
CFRelease(url);
CFRelease(myDictionary);// rendering code follows

Long story short I can't see if the PDF looks good cause I don't find it in iTunes under the File Sharing area. Am I missing something?


Not the final solution to your question but I would add something like NSFileManager createFileAtPath: or someObject writeToFile:file atomically:YES]; after the NSString * file row and verify if any file at all is created.

Run the app in the simulator, then open a terminal and do a

find ~/* | grep mypdf.pdf

That command should find the file under the Simulators documents directory. With a jailbroken device and a ssh shell you can try the same test on the phone, but for creating files the simulator tends to work ok.

Assuming you have set the UIFileSharingEnabled in info.plist your App will show in iTunes. And if mypdf.pdf is created, even when empty, it should show in iTunes, too. iTunes does not filter but will show anything that is located in the documents directory.

Once you have verified that a file can be created continue with stepping through the second code block you posted, using the debugger and see how the data is treated. I never created a PDF file so I cannot help you much there.


To view the PDF file, you can use webviews.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [docsDirectory stringByAppendingPathComponent:@"mypdf.pdf"];

// use the code for webView

UIWebView *attachmentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10,0,300,400)];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL fileURLWithPath:file]];   
[attachmentWebView loadRequest:request];
[request release];
[self.view addSubview:attachmentWebView];
[attachmentWebView release];
0

精彩评论

暂无评论...
验证码 换一张
取 消