Question

i've been hammering my brain trying to figure this one out and can't find anything in the doc's or on SO that is helpful so far. i have a project that allows the user the input data and save it to a plist. is there a way to display the data that has been stored in the plist in a new view in pdf format? what i am trying to do is to display the recorded data in a new view controller with pdf format so the user can print that list. i know there is a way but i just can't figure it out and i finally threw the towel in and here i am. i will be eternally grateful for any help guys. and girls too.

i can create a new pdf with the following code. i just can't seem to understand how to get the eta from the plist to display.

- (IBAction)didClickMakePDF {
[self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100];

[self beginPDFPage];

CGRect textRect = [self addText:@"This is some nice text here, don't you agree?" 
                      withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];

CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                                   withColor:[UIColor blueColor]];

UIImage *anImage = [UIImage imageNamed:@"tree.jpg"];
CGRect imageRect = [self addImage:anImage 
                          atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];

[self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
             withColor:[UIColor redColor]];

[self finishPDF];

}
Was it helpful?

Solution

So, you've got your PDF context and some text loaded from your plist. You need to decide how it will be laid out to be rendered into the PDF. Core Text can make a really nice job of it. The quick and easy route to get you started is:

start by flipping the context

CGContextScaleCTM(pdfContext, 1.0, -1.0);
CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height);

draw your text

[text drawAtPoint:CGPointMake(x, y) withFont:[UIFont boldSystemFontOfSize:48.0f]];

where you will obviously want to change:

  1. the text content in a loop
  2. the y position so each line is drawn further down the page
  3. the font

Images can be drawn into the context in the same way.

Then, move on to Core Text to do a better job with paragraphs of text.

OTHER TIPS

"When you draw to the PDF context using CGContext functions the drawing operations are recorded in PDF format. The PDF commands that represent the drawing are written to the destination specified when you create the PDF graphics context."

This comes from the same page referenced above: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html

A CGPDFContext is "just" a CGContext. You could set a color in it using "CGContextSetCMYKFillColor" for example or draw text in it using the NSString "drawInRect" method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top