Question

I'm currently creating and saving a PDF of a UIView. It is working great, but now I'm trying to create a PDF from text data saved to the disk rather than from the UIView. Ideally, I would like to create a PDF based on the text in the UITextView. The below code writes the first part of the text view just fine, but it doesn't create the PDF from the entire text view. It is quite a bit of text, so it only created the PDF from the amount of text that would fit on the view. The code that I'm currently using is this:

- (void)createPDFfromUIView:(UITextView*)view saveToDocumentsWithFileName:(NSString*)aFilename
{

   NSMutableDictionary *myDictionary = CFBridgingRelease(CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks));

    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, textview.bounds, myDictionary);
    UIGraphicsBeginPDFPage();

    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [textview.layer renderInContext:pdfContext]; // this line

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"filename"];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

And to call it / save it, I put this message send in an IBAction: [self createPDFfromUIView:textview saveToDocumentsWithFileName:@"pdflocation"];

Now, I've been parsing through this code and the docs for hours, and cannot figure out how to create a PDF based upon a saved text file rather than the view. I'm still new to programming, so a lot of this is going over my head. Does anyone know how ot change the above code to have it create a PDF based upon saved text rather than the view?

Options: I could create a PDF based on output of an NSString. I could also throw the saved text into a UITextView and create the PDF based upon that (original plan) I however don't know how to do either.

Was it helpful?

Solution

This article from Apple explains how to create Pdf files and draw text on Pdf pages.

OTHER TIPS

For those still looking for some sample code, this works:

- (NSData *)createPDFforText: (NSString *)text
{
    NSMutableData *pdfData = [NSMutableData data];
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
    UIGraphicsBeginPDFPage();

    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;

    [attributes setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
    [attributes setObject: [UIFont fontWithName: LATO_REGULAR size: 20.f] forKey: NSFontAttributeName];
    [attributes setObject: [UIColor blackColor] forKey: NSForegroundColorAttributeName];
    [text drawAtPoint: CGPointMake(20.f, 44.f) withAttributes: attributes];

    UIGraphicsEndPDFContext();

    return pdfData;

}

The CGRectZero bounds value sets the PDF document to the default page size of 8.5 by 11 inches (612 by 792 points).

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