title: Save UIWebView content as A4 sized PDF
body: <p>I have gone over previous questions touching upon the subject, but <strong>A4 Sized PDF</strong> is the troublesome part.</p>
  
  <p>The code I have written below saves a proper (proportional) PDF file, but its size (area wise) is 4 times of A4-Size. If I halve the height and width, I get an A4 sized PDF but the content overflows into two pages. </p>
  
  <p>How do I solve the problem? (fundamentally pertaining to scaling)</p>
  
  <pre><code>-(void)savePDFFromWebView:(UIWebView*)webView fileName:(NSString*)_fileName
  {
      int height, width, header, sidespace;
  
      height = 1754;
      width = 1240;
      header = 15;
      sidespace = 30;
  
      // set header and footer spaces
  
      UIEdgeInsets pageMargins = UIEdgeInsetsMake(header, sidespace, header, sidespace);  
  
      webView.viewPrintFormatter.contentInsets = pageMargins;
  
      UIPrintPageRenderer *renderer = [[UIPrintPageRenderer alloc] init];
  
      [renderer addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0];
  
      CGSize pageSize = CGSizeMake(width, height);
      CGRect printableRect = CGRectMake(pageMargins.left,
                                        pageMargins.top,
                                        pageSize.width - pageMargins.left - pageMargins.right,
                                        pageSize.height - pageMargins.top - pageMargins.bottom);
  
      CGRect paperRect = CGRectMake(0, 0, pageSize.width, pageSize.height);
  
      [renderer setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
      [renderer setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
  
  
      NSData *pdfData = [self printToPDFWithRender:renderer paperRect:paperRect];
  
  
      // save PDF file
      NSString *saveFileName = [NSString stringWithFormat:@"%@%dx%d.pdf", _fileName, (int)pageSize.width, (int)pageSize.height];
  
      NSString *savePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:saveFileName];
      [pdfData writeToFile: savePath  atomically: YES];
  }
  
  - (NSData*) printToPDFWithRenderer:(UIPrintPageRenderer*)renderer  paperRect:(CGRect)paperRect
  {
      NSMutableData *pdfData = [NSMutableData data];
  
      UIGraphicsBeginPDFContextToData( pdfData, paperRect, nil );
  
      [renderer prepareForDrawingPages: NSMakeRange(0, renderer.numberOfPages)];
  
      CGRect bounds = UIGraphicsGetPDFContextBounds();
  
      for ( int i = 0 ; i &lt; renderer.numberOfPages ; i++ )
      {
          UIGraphicsBeginPDFPage();
  
          [renderer drawPageAtIndex: i inRect: bounds];
      }
  
      UIGraphicsEndPDFContext();
  
      return pdfData;
  }
  </code></pre>