title: Contex Drawing + Pagination
body: <p>I am trying to draw contents of <code>scrollview</code> into a <code>PDF</code> context &amp; I am facing problem with <code>pagination</code>.</p>
  
  <p>Following Code I have used:  </p>
  
  <pre><code>- (void)renderTheView:(UIView *)view inPDFContext:(CGContextRef)pdfContext
  {
      // Creating frame.
      CGFloat heightOfPdf = [[[self attributes] objectForKey:SRCPdfHeight] floatValue];
      CGFloat widthOfPdf  = [[[self attributes] objectForKey:SRCPdfWidth] floatValue];
      CGRect pdfFrame = CGRectMake(0, 0, widthOfPdf, heightOfPdf);   
      CGRect viewFrame = [view frame];
  
      if ([view isKindOfClass:[UIScrollView class]])
      {
          viewFrame.size.height = ((UIScrollView *)view).contentSize.height;
          [view setFrame:viewFrame];
      }
      // Calculates number of pages.
      NSUInteger totalNumberOfPages = ceil(viewFrame.size.height/heightOfPdf);
  
      // Start rendering.
      for (NSUInteger pageNumber = 0; pageNumber&lt;totalNumberOfPages; pageNumber++)
      {
         // Starts our first page.
         CGContextBeginPage (pdfContext, &amp;pdfFrame);  
         // Turn PDF upsidedown
         CGAffineTransform transform = CGAffineTransformIdentity;
         transform = CGAffineTransformMakeTranslation(0,view.bounds.size.height);
         transform = CGAffineTransformScale(transform, 1.0, -1.0);
         CGContextConcatCTM(pdfContext, transform);
  
         // Calculate amount of y to be displace.
         CGFloat ty = (heightOfPdf*(pageNumber));
  
         CGContextTranslateCTM(pdfContext,0,-ty);
         [view.layer renderInContext:pdfContext];
  
         // We are done drawing to this page, let's end it.
         CGContextEndPage (pdfContext);
     }    
  }
  </code></pre>
  
  <p>It creates required number of pages but places the content wrongly. Following figure explains it.<img src="https://i.stack.imgur.com/DGpvp.png" alt="enter image description here">  </p>
  
  <p>Is there anything wrong in my code? </p>