title: Export UITableView to PDF
body: <p>I am struggling with a simple snippet of code, I've dedicated a lot of time searching for this, checked forums and tried different things. I am almost there but not quite. Basically <strong>I need to transform the contents of a UITableView to a PDF</strong>.</p>
  
  <p>The code I have right below, seems to work fine except that the user <em>has</em> to swipe the table to the end of it's content, if not that portion won't be rendered. I've scrolled the UITableView programatically but that doesn't seem to be doing the trick. I am getting more and more into Quartz2D to understand this stuff further, but the client needs this fixed for yesterday :-) </p>
  
  <pre><code>    -(void) createPDFfromUITable:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
  {
      CGRect priorBounds = self.tableView.bounds;
      CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width,3000)];
      self.tableView.bounds = CGRectMake(0, 0, fittedSize.width,1800);
  
      CGRect pdfPageBounds = CGRectMake(0, 0,self.view.bounds.size.width,792);
      NSMutableData *pdfData = [[NSMutableData alloc] init];
  
      UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil);
      CGFloat pageOriginY=0;
      for(int i=0;i&lt;2;i++)//for (CGFloat pageOriginY = 0; pageOriginY &lt; fittedSize.height; pageOriginY += pdfPageBounds.size.height)
      {
          UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil);
          CGContextSaveGState(UIGraphicsGetCurrentContext());
          {
              CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
              [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
              [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:([_resultados count]-1)] atScrollPosition:UITableViewScrollPositionTop animated:NO];
              CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY);
  
          } CGContextRestoreGState(UIGraphicsGetCurrentContext());
  
          pageOriginY += pdfPageBounds.size.height;
      }
  
      UIGraphicsEndPDFContext();
  
      self.tableView.bounds = priorBounds; // Reset the tableView
  
      NSArray     *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString    *documentsPath = [paths objectAtIndex:0];
      NSString    *filePathPDF = [documentsPath stringByAppendingPathComponent:aFilename];
      [pdfData writeToFile:filePathPDF atomically:YES];
      [self showEmail:filePathPDF];
  }
  </code></pre>
  
  <p>If you have suggestions for fixing the code I am using now, of you happen to have a whole snippet that does the whole thing, this would be much appreciated.</p>