【问题标题】:Generate PDF from UITableView,Plist in ios从 ios 中的 UITableView、Plist 生成 PDF
【发布时间】:2013-12-30 17:46:49
【问题描述】:

PList、UITableView中存储数据时如何生成PDF?

我对 ios 真的很陌生,而且也能生成 pdf

提前致谢。

【问题讨论】:

标签: ios uitableview pdf plist


【解决方案1】:

编辑:

假设您的 plist 结构如下:

================= 首先,您应该添加框架 coreText 和石英核心。他们导入并定义一些这样的宏:

#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>

#define LEFT_MARGIN 25
#define RIGHT_MARGIN 25
#define TOP_MARGIN 35
#define BOTTOM_MARGIN 50
#define BOTTOM_FOOTER_MARGIN 32
#define DOC_WIDTH 1004
#define DOC_HEIGHT 768

可以通过这种方式获取plist数据:

NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes" ofType:@"plist"];    

// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

//编辑

tableData = [dict objectForKey:@"RecipeName"];//tabledata is global array
self.textToDraw = [tableData objectAtIndex:0];

//编辑

但是,您可以通过以下代码创建动态 pdf:

 -(void) btnDrawPdf
{ 

 //    pageSize = CGSizeMake(612, 792); 

 NSString *fileName = @"DemoNetwrk.pdf"; 
 NSString *fontName = @"Helvetica"; 

// NSString *textToDraw = @"你的数据来自 plist";立即发表评论

//[self generatePdfWithFilePath:pdfFileName];
 [self createPDF:fileName **withContent:self.textToDraw**  forSize:14 forFontName:fontName andColor:[UIColor blackColor]];
}

//多页pdf和pdf会保存在doc目录库>Application Support>iPhoneSimulator>library>Documents(ios 4.3完美)

- (void) createPDF:(NSString *)fileName withContent:(NSString *)content forSize:(int)fontSize forFontName:(NSString *)fontName andColor:(UIColor *)color
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

CGRect a4Page = CGRectMake(0, 0, DOC_WIDTH, DOC_HEIGHT);

NSDictionary *fileMetaData = [[NSDictionary alloc] init];

if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData )) {
    NSLog(@"error creating PDF context");
    return;
}

BOOL done = NO;
CGContextRef context = UIGraphicsGetCurrentContext();

CFRange currentRange = CFRangeMake(0, 0);

CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSelectFont (context, [fontName cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);
CGContextSetFillColorWithColor(context, [color CGColor]);
// Initialize an attributed string.
CFMutableAttributedStringRef attrString = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringReplaceString (attrString, currentRange, (CFStringRef)content);

// Create the framesetter with the attributed string.
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);

do {
    UIGraphicsBeginPDFPage();



    CGMutablePathRef path = CGPathCreateMutable();

    CGRect bounds = CGRectMake(LEFT_MARGIN,
                               TOP_MARGIN,
                               DOC_WIDTH - RIGHT_MARGIN - LEFT_MARGIN,
                               DOC_HEIGHT - TOP_MARGIN - BOTTOM_MARGIN);

    CGPathAddRect(path, NULL, bounds);



    // Create the frame and draw it into the graphics context
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, currentRange, path, NULL);

    if(frame) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, bounds.origin.y);
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, 0, -(bounds.origin.y + bounds.size.height));
        CTFrameDraw(frame, context);
        CGContextRestoreGState(context);

        // Update the current range based on what was drawn.
        currentRange = CTFrameGetVisibleStringRange(frame);
        currentRange.location += currentRange.length;
        currentRange.length = 0;

        CFRelease(frame);
    }

     // If we're at the end of the text, exit the loop.
    if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)attrString))
        done = YES;
}
while(!done);

UIGraphicsEndPDFContext();

//    [fileMetaData release];  
 CFRelease(attrString);  
 CFRelease(framesetter);

}

我终于明白了:

它会帮助你...

【讨论】:

  • @如果你使用非arc然后取消注释[fileMetaData release];
  • NSString *textToDraw = @"你的plist数据";在这个我们从字典中获取
  • @user3045524 使用第一段代码,您可以从 plist 获取实际数据,但如果您想在没有 plist 的情况下进行测试,首先您应该获取类似 :NSString *textToDraw = @"your data from plist...这里复制粘贴一些大数据,先看看能不能getpdf";
  • @user3045524 如果您创建一个单独的课程来处理所有与 pdf 相关的内容,那将是一件好事,享受编码并继续提供帮助!!!!
【解决方案2】:

This 可能就是您要找的。​​p>

您应该在将 Plist 转换为 PDF 之前将其渲染到屏幕上(加载数据并将其显示在 UIView 或继承自 UIView 的类中。

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2012-05-26
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多