title: Compress an iOS PDF File?
body: <p>I have made an app that can make a PDF file.  I just have one image as the base, and layer text and other images on top of it to make the PDF.  The issue is the PDF is about 9 MB, and just one page.  Is there a way to compress the PDF down so its not so big?</p>
  
  <pre><code>- (IBAction)generatePdfButtonPressed:(id)sender
  {
      pageSize = CGSizeMake(792, 612);
      NSString *fileName = @"Demo.pdf";
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
  
      [self generatePdfWithFilePath:pdfFileName];
  }
  
  - (void) drawText
  {
      CGContextRef    currentContext = UIGraphicsGetCurrentContext();
      CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
  
      NSString *textToDraw = @"Text goes here";
  
      UIFont *font = [UIFont systemFontOfSize:22.0];
  
  
  
      CGRect renderingRect = CGRectMake(160, 177.82, 590, 95);
  
      [textToDraw drawInRect:renderingRect
                    withFont:font
               lineBreakMode:NSLineBreakByWordWrapping
                   alignment:NSTextAlignmentLeft];
  
  }
  
  - (void) drawImage
  {
      UIImage * demoImage = [UIImage imageNamed:@"demo.png"];
      [demoImage drawInRect:CGRectMake( 0, 0, 792, 612)];
  }
  
  - (void) generatePdfWithFilePath: (NSString *)thefilePath
  {
      UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
  
  
      BOOL done = NO;
      do
      {
  
          UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
  
  
           [self drawImage];
  
          [self drawText];
  
  
          done = YES;
      }
      while (!done);
  
      // Close the PDF context and write the contents out.
      UIGraphicsEndPDFContext();
  
  }
  </code></pre>