Obj-C View to PDF only displays the first page

I have a UIView extension class called UIView + PDFView that displays the current view, parses it, and creates a PDF document. My problem is part of the rendering; I can successfully create pages for content, however all pages after the first are blank. My ultimate goal is to take the current view, "scale" the width equal to the page, and split the rest of the view into a PDF file.

What is the problem:

  • Attach PDF to Email
  • Send PDF to printer / print simulator
  • Print only one page with the correct creation of a PDF file.
  • Correct resizing of a view (SO Question 4919388)
    • I do this before submitting a method. Checked when creating a 10px high frame, still prints one (and only one) full page
  • Correct translation of the submission. Validated by translation and scale; both correctly changed the view, but did not appear on more than the first page.

My code is as follows:

@interface UIView (PDFView)
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo;
@end

@implementation UIView (PDFView)

-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo {
    // http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html#//apple_ref/doc/constant_group/Auxiliary_Dictionary_Keys

    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    CGSize pageSize = CGSizeMake(self.bounds.size.width, 792);
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, documentInfo);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    NSInteger currentPage = 0;
    BOOL done = NO;

    do {
        CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);
        UIGraphicsBeginPDFPageWithInfo(currentPageRect, nil);

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

        // If we're at the end of the view, exit the loop.
        if ((pageSize.height*currentPage) > self.bounds.size.height) {
            done = YES;
        } else {
            currentPage++;
        }
    } while (!done);

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    if (aFilename == nil) {
        return pdfData;
    } else {
        // Retrieves the document directories from the iOS device
        NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

        NSString* documentDirectory = [documentDirectories objectAtIndex:0];
        NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

        // instructs the mutable data object to write its context to a file on disk
        [pdfData writeToFile:documentDirectoryFilename atomically:YES];
        NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
        return nil;
    }
}
+3
source share
1 answer

I think your problem lies

CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height);

try using any of the instructions below instead

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);
UIGraphicsBeginPDFPage();

every time you want to add a new page to the context, use the instructions above and you can add pages to the context.

if you want to use the default page size ie 612 X 792, you can directly use UIGraphicsBeginPDFPage();

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);

, .

+4

All Articles