Reputation: 1304
I'm working on my first app and am almost complete; I'm just stuck on a creating a PDF functionality. I have a simple Core Data Database and a Table View Controller (using FetchedResultsController) with a NavigationBar button item which when I press it, I ideally want a PDF to be created in the Documents Directory and then attached to the email. I'm at the stage of creating the PDF.
With following Apple's Guidelines (https://developer.apple.com/library/ios/documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html) but more of this tutorial here (http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/), I have a working PDF created in the Documents Directory of the iPhone Simulator.
Of course, the code in the tutorial above has a lot of methods that have been depreciated in iOS 7, so I have updated the "body text" methods to be:
- (void) drawText
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
NSString *textToDraw = @"Hello, this is a test PDF";
UIFont *font = [UIFont systemFontOfSize:14.0];
CGSize textSize = CGSizeMake(pageSize.width - 5*kBorderInset-5*kMarginInset, pageSize.height - 5*kBorderInset - 5*kMarginInset);
CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, textSize.height);
[textToDraw drawWithRect:renderingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
}
That's working very well. The PDF get's created with a border and under the heading line, it has the text saying "Hello, this is a test PDF" without the quotations.
That's all well and good, but the problem is extracting the information out from Core Data. This table view is calling my Core Data Entity called Transaction which has relationships to other Core Data Entities.
The table view controller (using a custom cell) has labels and an accessory view (an image) which I need to be passed over to the PDF. The table view is working really well; the PDF is working very well. The question is, how do I go about passing the core data information?
So I want to pass the "labels from the custom cells" into the PDF which is all getting it's information from Core Data.
Of course, it's not just one entry. This table view could have anything between 1 and 30 cells and each cell has 3 labels and an accessory view. I need to get those labels into Core Data.
I've tried playing around with the code, but I'm not sure how to format the NSString command above to get this working.
My saveToPDF button is:
- (IBAction)saveToPDF:(id)sender
{
pageSize = CGSizeMake(612, 792);
NSString *fileName = @"new.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
}
which calls:
- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
//NSInteger currentPage = 0;
BOOL done = NO;
do
{
//Start a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
//Draw a page number at the bottom of each page.
//currentPage++;
//[self drawPageNumber:currentPage];
//Draw a border for each page.
[self drawBorder];
//Draw text fo our header.
[self drawHeader];
//Draw a line below the header.
[self drawLine];
//Draw some text for the page.
[self drawText];
//Draw an image
[self drawImage];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
EDIT:
I created a fetchRequest within the drawText method and then output the information of that.
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *pdfFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
pdfFetchRequest.entity = entity;
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"dates.dateOfEvent" ascending:NO];
pdfFetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
pdfFetchRequest.fetchBatchSize = 20;
NSError *error = nil;
NSArray *types = [managedObjectContext executeFetchRequest:pdfFetchRequest error:&error];
NSString *textToDraw = [NSString stringWithFormat:@"Information = %@", types];
The output of this in the PDF is of course: " (entity: Transaction; id: 0x8baac90 ; data: {\n which is not readable for the user.
I have a custom cells with 3 labels and an accessory view. I would like either of the following:
1) The PDF to have a similar table view layout as the table view that called this extracting to PDF, so with cells, labels and accessory views (preferred outcome), or 2) A way to add code so that I draw a table with cells and have the cells and accessory view in the PDF.
Any insight or thoughts on getting the Core Data information into this PDF would be very welcomed!
Upvotes: 1
Views: 2636
Reputation: 1816
use NSFetchRequest
to get data from core data like this from apple example:
NSManagedObjectContext *context = [recipe managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"RecipeType" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSArray *types = [context executeFetchRequest:fetchRequest error:&error];
then you can iterate types
and create string for your pdf.
Upvotes: 1