title: How to write PDF data to a file first, when generating a PDF in Web View
body: <p>My app keeps receiving a memory warning and subsequent crash when generating a PDF. Ive been advised  to write my PDF  to a file first, but no expansion was offered.</p>
  
  <p>This is my first time on generating a PDF using iOS and despite checking out Apples documentation <a href="http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html" rel="nofollow">http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html</a> and searching SO, Google, and using <code>UIGraphicsBeginPDFContextToFile</code> I just keep getting warnings/no genration of PDF or exceptions thrown as im just guessing at this.  I really haven't grasped this concept yet.  </p>
  
  <p>Could someone offer some advice on how to write my PDF to a file first</p>
  
  <pre><code>- (void)viewWillAppear:(BOOL)animated
   {
   LogCmd();
  
  [super viewWillAppear:animated];
   if (self.pdfData != nil &amp;&amp; self.viewHasUnloaded == YES) {
  self.viewHasUnloaded = NO;
  [self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8"   baseURL:nil];
   }
  }
  
  
  
  - (void)viewDidAppear:(BOOL)animated
   {
  
    LogCmd();
   [super viewDidAppear:animated];
   if (self.pdfData == nil) {
  
  
  
  // Generate PDF
   [ICUtils showProgressViewWithTitle:@"Generating PDF. This may take a minute..."];
   [self performSelectorInBackground:@selector(generatePdf) withObject:nil];
    }
   }
  
   - (void)generatePdf
    {
    NSMutableArray *pagesArray = [NSMutableArray array];
  
   if ([self.certificate.certificateType.title isEqualToString:@"EICR"]) {
  [pagesArray addObject:[[ICPDFEICRPage1 alloc] initWithCertificate:self.certificate]];
  [pagesArray addObject:[[ICPDFEICRPage2 alloc] initWithCertificate:self.certificate]];
  [self addObservationsToPagesArray:pagesArray];
  [self addDistributionBoardsToPagesArray:pagesArray];
  [pagesArray addObject:[[ICPDFEICRInspection alloc] initWithCertificate:self.certificate]];
  [pagesArray addObject:[[ICPDFEICRInspectionPage1 alloc] initWithCertificate:self.certificate]];
  [pagesArray addObject:[[ICPDFEICRInspectionPage2 alloc] initWithCertificate:self.certificate]];
  [pagesArray addObject:[[ICPDFEICRInspectionPage3 alloc] initWithCertificate:self.certificate]];
  [pagesArray addObject:[[ICPDFEICRPageFinal alloc] initWithCertificate:self.certificate]];
      }
  
      // Set page count on all pages
       int pageNumber = 0;
       for (ICCertificateComponent *page in pagesArray) {
       page.pageNumber.text = [NSString stringWithFormat:@"%d", ++pageNumber];
       page.pageCount.text = [NSString stringWithFormat:@"%d", pagesArray.count];
     }
  
     NSData *pdfData = [self createPdfWithPages:pagesArray];
     [self performSelectorOnMainThread:@selector(pdfDone:) withObject:pdfData waitUntilDone:YES];
  
    }
   }
   - (void)pdfDone:(NSData *)data
   {
  self.pdfData = data;
  [self.webView loadData:self.pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8"   baseURL:nil];
  [ICUtils removeProgressView];
        }
  
  
  - (NSData *)createPdfWithPages:(NSArray *)pages
  
       {
      // Creates a mutable data object for updating with binary data, like a byte array
  
   NSMutableData *pdfData = [NSMutableData data];
  
   ICCertificateComponent *firstPage = [pages objectAtIndex:0];
  
   UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);
  
   for (int i = 0; i &lt; pages.count; i++) {
     ICCertificateComponent *thisPage = [pages objectAtIndex:i];
  UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);
  
  CGContextRef pdfContext = UIGraphicsGetCurrentContext();
  [thisPage.contentView.layer renderInContext:pdfContext];
     }
  
   UIGraphicsEndPDFContext();
  
   return pdfData;
  
   }
  </code></pre>
  
  <p><strong>What I have tried</strong></p>
  
  <pre><code> -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
   {
  
   NSArray *pages;
  // Creates a mutable data object for updating with binary data, like a byte array
          NSMutableData *pdfData = [NSMutableData data];
  
      ICCertificateComponent *firstPage = [pages objectAtIndex:0];
  
      UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);
  
      for (int i = 0; i &lt; pages.count; i++) {
          ICCertificateComponent *thisPage = [pages objectAtIndex:i];
          UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);
  
          CGContextRef pdfContext = UIGraphicsGetCurrentContext();
          [thisPage.contentView.layer renderInContext:pdfContext];
        }
  
      UIGraphicsBeginPDFPage();
     CGContextRef currentContext = UIGraphicsGetCurrentContext();
   animated:NO];
   [_webView.layer renderInContext:currentContext];
  
  
  
    UIGraphicsEndPDFContext();
  
     NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
  
     NSString* documentDirectory = [documentDirectories objectAtIndex:0];
     NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
  
    //return pdfData;
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
  }
  </code></pre>
  
  <p><strong>Error:</strong>  Exception thrown
   <code>'NSInvalidArgumentException', reason: '-[ICPDFPreviewController createPdfWithPages:]: unrecognized selector sent to instance</code> amongst others im sure</p>
  
  <p><strong>Also tried:</strong></p>
  
  <pre><code> - (NSString *)createPdfWithPages:(NSArray *)pages
   {
  
      @autoreleasepool {
  
  
  
      ICCertificateComponent *firstPage = [pages objectAtIndex:0];
  
      NSString *docDirectory =
      [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      NSString *pdfPath = [docDirectory stringByAppendingPathComponent:@"pdfFile.pdf"];
  
      UIGraphicsBeginPDFContextToFile(pdfPath, firstPage.contentView.bounds, nil);
  
      for (int i = 0; i &lt; pages.count; i++) {
          ICCertificateComponent *thisPage = [pages objectAtIndex:i];
          UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);
  
          CGContextRef pdfContext = UIGraphicsGetCurrentContext();
          [thisPage.contentView.layer renderInContext:pdfContext];
                 }
  
      UIGraphicsEndPDFContext();
  
      return pdfPath;
  
  }
  </code></pre>
  
  <p>error: <code>NSInvalidArgumentException', reason: '-[NSPathStore2 bytes]: unrecognized selector sent to instance 0x24aa4580'</code></p>