2016-06-21 80 views
0

我正在用一段簡單的代碼掙扎,我已經花了很多時間尋找這個,檢查論壇並嘗試了不同的東西。我幾乎在那裏,但不完全。基本上我需要將UITableView的內容轉換爲PDFConvertir UITableView to PDF

下面的代碼似乎正常工作,除非用戶已將劃過表格內容的末尾,否則該部分將不會呈現。我以編程方式滾動了UITableView,但似乎並沒有這樣做。我越來越多地進入Quartz2D進一步瞭解這個東西,但客戶需要這個固定昨日:-)

-(void) createPDFfromUITable:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
    CGRect priorBounds = self.tableView.bounds; 
    CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width,3000)]; 
    self.tableView.bounds = CGRectMake(0, 0, fittedSize.width,1800); 

    CGRect pdfPageBounds = CGRectMake(0, 0,self.view.bounds.size.width,792); 
    NSMutableData *pdfData = [[NSMutableData alloc] init]; 

    UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); 
    CGFloat pageOriginY=0; 
    for(int i=0;i<2;i++)//for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height) 
    { 
     UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); 
     CGContextSaveGState(UIGraphicsGetCurrentContext()); 
     { 
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY); 
      [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
      [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:([_resultados count]-1)] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY); 

     } CGContextRestoreGState(UIGraphicsGetCurrentContext()); 

     pageOriginY += pdfPageBounds.size.height; 
    } 

    UIGraphicsEndPDFContext(); 

    self.tableView.bounds = priorBounds; // Reset the tableView 

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePathPDF = [documentsPath stringByAppendingPathComponent:aFilename]; 
    [pdfData writeToFile:filePathPDF atomically:YES]; 
    [self showEmail:filePathPDF]; 
} 

如果您有任何建議固定我現在使用的,你碰巧有碼整個片段完成整個事情,這將非常感激。

回答

1

我遇到了類似的情況,我想導出用戶輸入的數據以生成包含該信息的pdf報告。

經過幾天的研究,我發現屏幕截圖UITableView並不是一個好習慣。

我推薦使用CoreText來繪製它自己的。這裏是輔助方法來完成繪圖。

+(void)drawText:(NSString*)textToDraw withFont:(UIFont *)font inFrame:(CGRect)frameRect 
{ 
    if (textToDraw) { 
     // create attributed string 
     CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
     CFAttributedStringReplaceString (attrStr, CFRangeMake(0, 0), (CFStringRef) textToDraw); 

     // create font 
     if (!font) { 
      font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:12]; 
     } 
     CFStringRef fontString = (__bridge CFStringRef)font.fontName; 

     CTFontRef fontRef = CTFontCreateWithName(fontString, font.pointSize, NULL); 

     // create paragraph style and assign text alignment to it 
     CTTextAlignment alignment = kCTTextAlignmentLeft; 
     CTParagraphStyleSetting _settings[] = { {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} }; 
     CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings)/sizeof(_settings[0])); 

     // set paragraph style attribute 
     CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTParagraphStyleAttributeName, paragraphStyle); 

     // set font attribute 
     CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, fontRef); 

     // release paragraph style and font 
     CFRelease(paragraphStyle); 
     CFRelease(fontRef); 

     // Prepare the text using a Core Text Framesetter 

     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr); 


     CGMutablePathRef framePath = CGPathCreateMutable(); 
     CGPathAddRect(framePath, NULL, frameRect); 

     // Get the frame that will do the rendering. 
     CFRange currentRange = CFRangeMake(0, 0); 
     CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
     CGPathRelease(framePath); 

     // Get the graphics context. 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

     // Put the text matrix into a known state. This ensures 
     // that no old scaling factors are left in place. 
     CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


     // Core Text draws from the bottom-left corner up, so flip 
     // the current transform prior to drawing. 
     CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
     CGContextScaleCTM(currentContext, 1.0, -1.0); 

     // Draw the frame. 
     CTFrameDraw(frameRef, currentContext); 

     CGContextScaleCTM(currentContext, 1.0, -1.0); 
     CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 



    } 
} 

+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 1.0); 

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 



    CGColorRef color = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor; 

    CGContextSetStrokeColorWithColor(context, color); 


    CGContextMoveToPoint(context, from.x, from.y); 
    CGContextAddLineToPoint(context, to.x, to.y); 

    CGContextStrokePath(context); 
    CGColorSpaceRelease(colorspace); 


} 

+(void)drawTableAt:(CGPoint)origin 
    withRowHeight:(int)rowHeight 
    andColumnWidth:(int)columnWidth 
     andRowCount:(int)numberOfRows 
    andColumnCount:(int)numberOfColumns 

{ 
    for (int i = 0; i <= numberOfRows; i++) { 

     int newOrigin = origin.y + (rowHeight*i); 


     CGPoint from = CGPointMake(origin.x, newOrigin); 
     CGPoint to = CGPointMake(origin.x + (numberOfColumns*columnWidth), newOrigin); 

     [self drawLineFromPoint:from toPoint:to]; 


    } 

    for (int i = 0; i <= numberOfColumns; i++) { 
     int newOrigin = origin.x + (columnWidth*i); 


     CGPoint from = CGPointMake(newOrigin, origin.y); 
     CGPoint to = CGPointMake(newOrigin, origin.y +(numberOfRows*rowHeight)); 

     [self drawLineFromPoint:from toPoint:to]; 


    } 
} 

所以,你的代碼將是這樣的:

// Draw first page 
CGRect pageFrame = CGRectMake(0, 0, 612.0, 792.0); 
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil); 
CGContextScaleCTM(UIGraphicsGetCurrentContext(),pageFrame.size.width/screenSize.width, pageFrame.size.height/screenSize.height); 
[self doTheDrawing]; 
UIGraphicsEndPDFContext();