Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contex Drawing + Pagination

I am trying to draw contents of scrollview into a PDF context & I am facing problem with pagination.

Following Code I have used:

- (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<totalNumberOfPages; pageNumber++)     {        // Starts our first page.        CGContextBeginPage (pdfContext, &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);    }     } 

It creates required number of pages but places the content wrongly. Following figure explains it.enter image description here

Is there anything wrong in my code?

like image 393
Samyag Shah Avatar asked Sep 30 '11 07:09

Samyag Shah


2 Answers

I don't think you need the code which flips the PDF upside down. I've done this before (manual pagination, but without a scroll view) and never had to flip the context vertically. My main concern is that you are doing it on every iteration - if you have a valid reason for flipping it, you probably don't need it in the loop, but just once before the loop begins. Also the code CGContextTranslateCTM(pdfContext,0,-ty); might need to be replaced with CGContextTranslateCTM(pdfContext,0,heightOfPdf);

If that doesn't work, try UIGraphicsBeginPDFPage(); instead of CGContextBeginPage(), that's the only major difference between your code and mine.

like image 153
zeroimpl Avatar answered Sep 29 '22 12:09

zeroimpl


This github project can definitely help you.

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx { CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1); CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),                                         CGContextGetClipBoundingBox(ctx)); CGContextConcatCTM(ctx, transform); CGContextDrawPDFPage(ctx, page); 

}

like image 20
YogiAR Avatar answered Sep 29 '22 10:09

YogiAR