私は、複数ページのPDFを作成し、plistからデータを作成し、そのようなPDFを電子メールで送信する必要があるアプリケーションを開発しています。PDFに変換されるコンテンツには、テキストといくつかの画像が含まれています。私はこれに関するいくつかの投稿を見ましたが、それからあまり収集することができませんでした。誰かがこれを導くことができれば素晴らしいでしょう。
これが私が行ったPDF作成のコードです。
// create some sample data. In a real application, this would come from the database or an API.
NSString* path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"];
NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"collections %@",data);
NSArray* students = [data objectForKey:@"Students"];
NSLog(@"collections students %@",students);
// get a temprorary filename for this PDF
path = NSTemporaryDirectory();
NSLog(@"path here %@",path);
self.pdfFilePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.pdf", [[NSDate date] timeIntervalSince1970]]];
NSLog(@"pdf file path and Name %@",self.pdfFilePath);
// Create the PDF context using the default page size of 612 x 792.
// This default is spelled out in the iOS documentation for UIGraphicsBeginPDFContextToFile
UIGraphicsBeginPDFContextToFile(self.pdfFilePath, CGRectZero, nil);
// get the context reference so we can render to it.
CGContextRef context = UIGraphicsGetCurrentContext();
int currentPage = 0;
// maximum height and width of the content on the page, byt taking margins into account.
CGFloat maxWidth = kDefaultPageWidth - kMargin * 2;
CGFloat maxHeight = kDefaultPageHeight - kMargin * 2;
// we're going to cap the name of the class to using half of the horizontal page, which is why we're dividing by 2
CGFloat classNameMaxWidth = maxWidth / 3;
// the max width of the grade is also half, minus the margin
CGFloat gradeMaxWidth = (maxWidth / 3) - kColumnMargin;
CGFloat yearMaxWidth = (maxWidth / 3) - kColumnMargin;
// only create the fonts once since it is a somewhat expensive operation
UIFont* studentNameFont = [UIFont boldSystemFontOfSize:17];
UIFont* classFont = [UIFont systemFontOfSize:15];
CGFloat currentPageY = 0;
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
currentPageY = kMargin;
// iterate through out students, adding to the pdf each time.
for (NSDictionary* student in students)
{
// every student gets their own page
// Mark the beginning of a new page.
NSString* title = [NSString stringWithFormat:@"Order List"];
CGSize size2 = [title sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[title drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size2.height;
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextMoveToPoint(context, kMargin, currentPageY);
CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY);
CGContextStrokePath(context);
// draw the student's name at the top of the page.
NSString* name = [NSString stringWithFormat:@"%@ %@",
[student objectForKey:@"FirstName"],
[student objectForKey:@"LastName"]];
CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size.height;
NSString* dob = [NSString stringWithFormat:@"Grade"];
CGSize size1 = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[dob drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth + kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size1.height;
NSString* year = [NSString stringWithFormat:@"Years"];
CGSize size3 = [dob sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap];
[year drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth +gradeMaxWidth+ kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap];
currentPageY += size3.height;
// draw a one pixel line under the student's name
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGContextMoveToPoint(context, kMargin, currentPageY);
CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY);
CGContextStrokePath(context);
// iterate through the list of classes and add these to the PDF.
NSArray* classes = [student objectForKey:@"Classes"];
for(NSDictionary* class in classes)
{
NSString* className = [class objectForKey:@"Name"];
NSString* grade = [class objectForKey:@"Grade"];
NSString *year=[class objectForKey:@"Year"];
// before we render any text to the PDF, we need to measure it, so we'll know where to render the
// next line.
size = [className sizeWithFont:classFont constrainedToSize:CGSizeMake(classNameMaxWidth, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
// if the current text would render beyond the bounds of the page,
// start a new page and render it there instead
if (size.height + currentPageY > maxHeight) {
// create a new page and reset the current page's Y value
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
currentPageY = kMargin;
}
// render the text
[className drawInRect:CGRectMake(kMargin, currentPageY, classNameMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
// print the grade to the right of the class name
[grade drawInRect:CGRectMake(kMargin + classNameMaxWidth + kColumnMargin, currentPageY, gradeMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
[year drawInRect:CGRectMake(kMargin +classNameMaxWidth*2+ kColumnMargin , currentPageY, yearMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft];
currentPageY += size.height;
}
// increment the page number.
currentPage++;
}