title: Create PDF Document from Core Data and Table View Controller
body: <p>I'm working on my first app and am almost complete; I'm just stuck on a creating a PDF functionality. I have a simple Core Data Database and a Table View Controller (using FetchedResultsController) with a NavigationBar button item which when I press it, I ideally want a PDF to be created in the Documents Directory and then attached to the email. I'm at the stage of creating the PDF. </p>
  
  <p>With following Apple's Guidelines (<a href="https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html" rel="nofollow">https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html</a>) but more of this tutorial here (<a href="http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/" rel="nofollow">http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/</a>), I have a working PDF created in the Documents Directory of the iPhone Simulator. </p>
  
  <p>Of course, the code in the tutorial above has a lot of methods that have been depreciated in iOS 7, so I have updated the "body text" methods to be:</p>
  
  <pre><code>- (void) drawText
  {
      CGContextRef    currentContext = UIGraphicsGetCurrentContext();
      CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
  
      NSString *textToDraw = @"Hello, this is a test PDF";
  
      UIFont *font = [UIFont systemFontOfSize:14.0];
  
      CGSize textSize = CGSizeMake(pageSize.width - 5*kBorderInset-5*kMarginInset, pageSize.height - 5*kBorderInset - 5*kMarginInset);
  
      CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, textSize.height);
  
      [textToDraw drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
  }
  </code></pre>
  
  <p>That's working very well. The PDF get's created with a border and under the heading line, it has the text saying "Hello, this is a test PDF" without the quotations. </p>
  
  <p>That's all well and good, but the problem is extracting the information out from Core Data. This table view is calling my Core Data Entity called Transaction which has relationships to other Core Data Entities. </p>
  
  <p>The table view controller (using a custom cell) has labels and an accessory view (an image) which I need to be passed over to the PDF. The table view is working really well; the PDF is working very well. The question is, how do I go about passing the core data information?</p>
  
  <p>So I want to pass the "labels from the custom cells" into the PDF which is all getting it's information from Core Data. </p>
  
  <p>Of course, it's not just one entry. This table view could have anything between 1 and 30 cells and each cell has 3 labels and an accessory view. I need to get those labels into Core Data. </p>
  
  <p>I've tried playing around with the code, but I'm not sure how to format the NSString command above to get this working. </p>
  
  <p>My saveToPDF button is:</p>
  
  <pre><code>- (IBAction)saveToPDF:(id)sender
  {
      pageSize = CGSizeMake(612, 792);
      NSString *fileName = @"new.pdf";
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
  
      [self generatePdfWithFilePath:pdfFileName];
  
  }
  </code></pre>
  
  <p>which calls:</p>
  
  <pre><code>- (void) generatePdfWithFilePath: (NSString *)thefilePath
  {
      UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
  
      //NSInteger currentPage = 0;
      BOOL done = NO;
      do
      {
          //Start a new page.
          UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
  
          //Draw a page number at the bottom of each page.
          //currentPage++;
          //[self drawPageNumber:currentPage];
  
          //Draw a border for each page.
          [self drawBorder];
  
          //Draw text fo our header.
          [self drawHeader];
  
          //Draw a line below the header.
          [self drawLine];
  
          //Draw some text for the page.
          [self drawText];
  
          //Draw an image
          [self drawImage];
          done = YES;
      }
      while (!done);
  
      // Close the PDF context and write the contents out.
      UIGraphicsEndPDFContext();
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I created a fetchRequest within the drawText method and then output the information of that. </p>
  
  <p>NSManagedObjectContext *managedObjectContext = [self managedObjectContext];</p>
  
  <pre><code>NSFetchRequest *pdfFetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
  pdfFetchRequest.entity = entity;
  NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dates.dateOfEvent" ascending:NO];
  pdfFetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
  pdfFetchRequest.fetchBatchSize = 20;
  NSError *error = nil;
  NSArray *types = [managedObjectContext executeFetchRequest:pdfFetchRequest error:&amp;error];
  NSString *textToDraw = [NSString stringWithFormat:@"Information = %@", types];
  </code></pre>
  
  <p>The output of this in the PDF is of course: " (entity: Transaction; id: 0x8baac90  ; data: {\n which is not readable for the user. </p>
  
  <p>I have a custom cells with 3 labels and an accessory view. I would like either of the following:</p>
  
  <p>1) The PDF to have a similar table view layout as the table view that called this extracting to PDF, so with cells, labels and accessory views (preferred outcome), or
  2) A way to add code so that I draw a table with cells and have the cells and accessory view in the PDF. </p>
  
  <p>Any insight or thoughts on getting the Core Data information into this PDF would be very welcomed!</p>