1

I created an app that generates a PDF and saves it to the NSDocumentDirectory. I want to make it very easy for the user to locate the file and display it or print it using their iPhone. How do I find this file and view it easily?

Here is the code I used to create the PDF:

       //USE PDFGENERATOR TO CREATE A PDF FILE
    PDFViewController* pdf = [[PDFViewController alloc] init];

    NSString *header = lblTitle.text;
    NSLog(@"fullQuote=%@", header);

    NSString *body = [NSString stringWithFormat:@"%@ \n\n - LRH \n %@", lblExcerpt.text, lblDesc2.text];
    NSLog(@"fullQuote=%@", body);

    pageSize = CGSizeMake(612, 792);
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", lblTitle.text];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    [pdf generatePdf :pdfFileName:header:body:nil];
jroyce
  • 2,029
  • 2
  • 22
  • 45

1 Answers1

0

There is a good tutorial available here which demonstrates how to open .pdf, .xls files in your application.

The main class you have to refer for this is QLPreviewController. here

This is the Datasource Method you would have to call for that

- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{
   // Break the path into it's components (filename and extension)
   NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

  // Use the filename (index 0) and the extension (index 1) to get path

  NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];

 return [NSURL fileURLWithPath:path];
}

Also someone would like to refer this SO question :iPhone - Opening word and excel file without using UIWebview.

Community
  • 1
  • 1
rohan-patel
  • 5,772
  • 5
  • 45
  • 68
  • Thank you and this is very helpful. I also would like simple direction on how to find the file, like if the person created the PDF and now wants to look at it external to my app, or access the file in some other manner on the filesystem. Is something like that even possible? – jroyce Mar 01 '12 at 05:55