PDF Generation From UIScrollView + iphone

后端 未结 3 1647
半阙折子戏
半阙折子戏 2021-01-03 11:08

I want to generate a pdf of UIScrollView whose content size is near about 320*2000. If I use current graphic image context to capture the scroll view layer, then it only cap

相关标签:
3条回答
  • 2021-01-03 11:35

    is it possible to see your sample code please, I am trying to do the same exactly thing.

    you could send the source code here, thanks for that you would be very generous dott.marco@mac.com

    anyway for your problem maybe this can help:

    You are capturing the visible content only because your scroll view is sized match the visible content only, which it should be. You need to resize your scrollview to match the content you need to capture before you start the drawing code using something like this:

    Code: self.scrollView.frame = CGRectMake(0, 0, "your content width here", "your content height here"); After you are done drawing make sure to resize your scroll view to back, in this case to the container view:

    Code: self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    0 讨论(0)
  • 2021-01-03 11:57

    Heh guys, i prepared some code base using the examples available on net to create PDF from UIScrollView We should be clear about two things

    1) number of pages 2) contentsize of scrollview

    i have created a scroll view of content size ( 10 * 910). so it has 10 pages.. This is for iPad.

    here is the code base

    - (void) createPDF
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *directroyPath = nil;
        directroyPath = [documentsDirectory stringByAppendingPathComponent:@"PDF"];
        NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
        // check for the "PDF" directory
        NSError *error;
        if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    
        } else {
            [[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
                                      withIntermediateDirectories:NO
                                                       attributes:nil
                                                            error:&error];
        }   
    
         CGContextRef pdfContext = [self createPDFContext:myScrollView.bounds path:(CFStringRef)filePath];
        NSLog(@"PDF Context created");
    
        for (int i = 0 ; i< 10 ; i++)
        {
    
            // page 1
            CGContextBeginPage (pdfContext,nil); 
    
            //turn PDF upsidedown   
            CGAffineTransform transform = CGAffineTransformIdentity;    
            transform = CGAffineTransformMakeTranslation(0, (i+1) * 910);   
            transform = CGAffineTransformScale(transform, 1.0, -1.0);   
            CGContextConcatCTM(pdfContext, transform);  
    
            //Draw view into PDF
            [myScrollView.layer renderInContext:pdfContext];    
            CGContextEndPage (pdfContext);          
            [myScrollView setContentOffset:CGPointMake(0, (i+1) * 910) animated:NO];
    
        }
        CGContextRelease (pdfContext);
    }
    
    - (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path    
    {   
        CGContextRef myOutContext = NULL;   
        CFURLRef url;   
        url = CFURLCreateWithFileSystemPath (NULL, path,                                         
                                             kCFURLPOSIXPathStyle,                                       
                                             false);
    
        if (url != NULL) {      
            myOutContext = CGPDFContextCreateWithURL (url, 
                                                      &inMediaBox,                                                
                                                      NULL);        
            CFRelease(url);     
        }   
        return myOutContext;
    }
    
    0 讨论(0)
  • 2021-01-03 11:57

    I came up with a different solution:

    -(NSData*)pdfDataFromTableViewContent{
       NSMutableData *pdfData = [NSMutableData data];
       //The real size, not only the visible part
       CGSize contentSize = self.tableView.contentSize;
       CGRect tableViewRealFrame = self.tableView.frame;
       //Size tableView to show the whole content
       self.tableView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height);
       //Generate PDF (one page)
       UIGraphicsBeginPDFContextToData(pdfData,self.tableView.frame, nil);
       UIGraphicsBeginPDFPage();
       [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
       UIGraphicsEndPDFContext();
       //Resize frame
       self.tableView.frame = tableViewRealFrame;
       return  pdfData;}
    

    Size the frame to fit the entire content of the scrollView, capture it in graphicsContext, size the frame back.

    The upper code generates one page.
    For more pages I use basically this:

    //MultiPage
    CGRect mediaBox = self.tableView.frame;
    CGSize pageSize = CGSizeMake(self.view.frame.size.width, 800);
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();
    NSInteger currentPage = 0;
    BOOL done = NO;
    do {
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);
        CGContextTranslateCTM(pdfContext, 0, -(pageSize.height*currentPage));
        [self.view.layer renderInContext:pdfContext];
        if ((pageSize.height*(currentPage+1)) > mediaBox.size.height) done = YES;
        else currentPage++;
    } while (!done);
    UIGraphicsEndPDFContext();
    
    0 讨论(0)
提交回复
热议问题