Note that there are some explanatory texts on larger screens.

plurals
  1. POObj-C View to PDF only renders first page
    primarykey
    data
    text
    <p>I've got a UIView extension class called UIView+PDFView which takes the current view, splits it into pages, and renders a PDF document. My issue lies in the rendering part; I can successfully 'create' pages for content, however all pages after the first are blank. My end goal is to take the current view, 'scale' the width equal to the page, and paginate the rest of the view within a PDF.</p> <p>What the issue isn't:</p> <ul> <li>Attaching the PDF to e-mail</li> <li>Sending the PDF to a printer/print simulator</li> <li>Only printing one page while actually generating the PDF correctly</li> <li>Correctly resizing the view <a href="https://stackoverflow.com/questions/4919388/generate-a-pdf-from-a-view-for-emailing">(SO Question 4919388)</a> <ul> <li>I do this before I send to method. Verified by making frame 10px tall, still prints one (and only one) full page</li> </ul></li> <li>Correctly translating the view. Verified by making both a translation and a scale; both correctly changed the view, however neither rendered on more than the first page.</li> </ul> <p>My code is as follows:</p> <pre><code>@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) &gt; 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload