iosobjective-cpdfprintinggraphicscontext

Split UIView/graphics context into areas


I am trying to print a UIView as a PDF.

The problem is that if the UIView is taller than a page it gets cut off.

So what I want to do is iterate over "page heights" of the UIView and render them each as a PDF page.

Here is my code:

UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 400, 782), nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();

UIGraphicsBeginPDFPage();

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[self.receiptContentView.layer renderInContext:pdfContext];

UIGraphicsBeginPDFPage();

// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[self.receiptContentView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

Obviously at the moment its just rendering the same thing on each page.

How would I change my code above to say "only print the first 782px on Page 1 then print the NEXT 782px on page 2"?

Many thanks.


Solution

  • Try transforming the context upward when starting each page.

    CGContextBeginPDFPage();
    CGContextTranslateCTM(pdfContext, 0.0, -782.0);
    [self.receiptContentView.layer renderInContext:pdfContext];