2

How to Generate PDF when data is stored in PList , UITableView?

I am really new to ios and that too generate pdf

Thanks In Advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vara
  • 989
  • 1
  • 7
  • 16
  • http://stackoverflow.com/questions/4362734/ios-sdk-programmatically-generate-a-pdf-file – amar Dec 12 '13 at 10:57

2 Answers2

2

edit:

suppose your plist is structured as : your plist suppose named recipes

================ At very first you should add frameworks coreText and quartz core. them import and define some macro like this:

#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

you can get plist data in this way:

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

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

//edit

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

//edit

however below by below code you can create dynamic pdf:

 -(void) btnDrawPdf
{ 

 //    pageSize = CGSizeMake(612, 792); 

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

// NSString *textToDraw = @"your data from plist "; comment this now

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

//multi page pdf and pdf will be saved in doc directory library>Application Support>iPhoneSimulator>library>Documents (perfect in 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);

}

finally i am getting this: enter image description here

it will help you...

maddy
  • 4,001
  • 8
  • 42
  • 65
  • @if you are using non-arc then uncomment [fileMetaData release]; – maddy Dec 12 '13 at 11:30
  • NSString *textToDraw = @"your data from plist "; In this we are getting from dictionary – Vara Dec 12 '13 at 11:33
  • @user3045524 using the very first segment code you can get actual data from plist, still if you want to test without plist, firstly you should take dummy data like :NSString *textToDraw = @"your data from plist...copy and paste some big data here, firstlt check if you are able to getpdf "; – maddy Dec 12 '13 at 11:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43040/discussion-between-alok-and-user3045524) – maddy Dec 12 '13 at 11:44
  • 1
    @user3045524 it will be good if you create a seperate class dealing with all your pdf related stuff, enjoy coding and keep helping !!!! – maddy Dec 12 '13 at 14:00
0

This might be what you are looking for.

You should render your Plist to screen before converting it to PDF (load the data and displaying it in a UIView or a class inherited from UIView.

Community
  • 1
  • 1
Konrad77
  • 2,515
  • 1
  • 19
  • 36