title: Generate a PDF from a view for emailing
body: <p>I have a view with several labels and text views. I am trying to generate a PDF file for that page/view and attach to an email.</p>
  <p>First, I tried this approach and it seems either the file is not created or not attached to the email. Here are my codes:</p>
  <pre><code>NSMutableData *pdfData = [NSMutableData data];
  UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
  UIGraphicsBeginPDFPage();
  
  [self.view drawRect:self.view.bounds];
  
  UIGraphicsEndPDFContext();
  
  MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
  mailComposer.mailComposeDelegate = self;
  
  
  
  [pdfData writeToFile:file atomically:YES];
  
  
  
  [mailComposer addAttachmentData:pdfData mimeType:@&quot;application/pdf&quot; fileName:file];
  
  [self presentModalViewController:mailComposer animated:YES];
  </code></pre>
  <p>And since that didn't work, I tried  a different approach by creating the PDF separately first, then attach it to the email. Then I saw that the PDF file is created at that directory, but it is empty!!!</p>
  <p>Here are my revised codes:</p>
  <pre><code>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *file = [documentsDirectory stringByAppendingFormat:@&quot;/tempFile.pdf&quot;];
  
  UIGraphicsBeginPDFContextToFile(file, self.view.bounds, nil);
  UIGraphicsBeginPDFPage();
  
  [self.view drawRect:self.view.bounds];
  
  UIGraphicsEndPDFContext();
  
  MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
  mailComposer.mailComposeDelegate = self;
  
  
  
  [pdfData writeToFile:file atomically:YES];
  
  
  
  [mailComposer addAttachmentData:pdfData mimeType:@&quot;application/pdf&quot; fileName:file];
  
  
  [self presentModalViewController:mailComposer animated:YES];
  </code></pre>
  <ul>
  <li>Can you please help me to successfully create a PDF from a view, then attach it to the email?</li>
  </ul>