1

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!

3
  • What is the end result you are looking for, e.g., are you trying to capture the table view in your PDF document? Commented Nov 27, 2013 at 12:50
  • Thanks for the reply - Ideally yes - to have the PDF show the Table View with the information would be the best here, but I don't know if that would be possible, so I may have to just use formatting to separating out the information, but yes, having the Table View custom cells showcased would be the best approach here and most appropriate if possible for the user Commented Nov 27, 2013 at 13:08
  • I have just updated my original question to include the current outcome Commented Nov 27, 2013 at 13:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks very much Hashmat and that does make sense - Within this class, I already have a FetchedRequest which is what is pulling in that information for the table view - is it ok to have another fetchRequest in the same class, of course, doing something different or could I just use that existing fetchRequest? Also, what would the code look like to iterate through types for the string?
of course you can have different fetchrequest with different parameters. i recommend set the parameters according to your need. please accept the answer if you think it helped to solve the problem.
Thanks - we're getting there - I'm iterating through "types" with NSString *textToDraw = [NSString stringWithFormat:@"Information = %@", types]; and am getting "<Transaction: 0x8bacf70> (entity: Transaction; id: 0x8baac90 <x-coredata:// 84E1413F-F772-403F-B8A8-722C2656069E/Transaction/p4> ; data: {\n as the Output in the PDF, which is understandable because that is the Raw Core Data information, but of course that makes no sense to the user. I would like the actual information out of the custom cells.. so the labels in the custom cells in a table like view if possible - is that possible?
that would be possible for current visible cells. if that's what you wish
just build the string lke the you show the information in cells but in one string for your pdf. dont just print nsmanagedobject. that you will get pointer address. please read core data documentations
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.