Monday 22 September 2014

Generate PDF file for Export the Image’s & Data’s

.h File
@interface ViewController : UIViewController<UIDocumentInteractionControllerDelegate>
{
UIAlertView *Alert;
}
@end

.m File
-(IBAction)Send_Mail:(id)sender
{
[self generate_PDF];

   if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;
        [mailComposer setSubject:@"Results"];
        
        NSString* fileName = @"Result.pdf";
        
        NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *path = [arrayPaths objectAtIndex:0];
        NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
        NSData *myData = [NSData dataWithContentsOfFile:pdfFileName];
        
        [mailComposer addAttachmentData:myData mimeType:@"application/pdf" fileName:fileName];
        
        [self presentViewController:mailComposer animated:YES completion:nil];
    }
    
    else
    {
        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"No Mail Accounts" message:@"Please set up a Mail account in order to send email." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

-(IBAction)Save_Pdf:(id)sender
{
[self generate_PDF];
    [self open_pdf];
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            Alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"E-Mail Sending Failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)generate_PDF
{
    NSString *docDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *pdfPath = [docDirectory stringByAppendingPathComponent:@"Result.pdf"];

    // set the size of the page (the page is in landscape format)
    CGRect pageBounds = CGRectMake(0, 0, self.Scrollview.contentSize.widthself.Scrollview.contentSize.height);

    // sets the size and position of the image and text
    CGRect imageBounds = CGRectMake(100, 100, self.Scrollview.contentSize.widthself.Scrollview.contentSize.height);
    CGRect textBounds = CGRectMake(0.0f, 0.0f, 182.0f, 375.0f);
    
    
    UIImage *image_val = [UIImage imageNamed:@"results_page.png"];
    UILabel *textLabel = [[UILabel alloc]init];

    //set the image (or) snapshot image (or) any other image
    UIImage *theImage = image_val;

    // set the text (or) data from Array
    textLabel.text = @"Test the values";

    // create and save the pdf file
    UIGraphicsBeginPDFContextToFile(pdfPath, pageBounds, nil);
    {
        UIGraphicsBeginPDFPage();
        [textLabel drawTextInRect:textBounds];
        [theImage drawInRect:imageBounds];
        
    }
    UIGraphicsEndPDFContext();
}


-(void)open_pdf
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Result.pdf"];
    
    NSURL *url = [NSURL fileURLWithPath:filePath];
    UIDocumentInteractionController *docController = [UIDocumentInteractionController alloc];
    docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    docController.delegate = self;
    BOOL isValid = [docController presentOpenInMenuFromRect:self.Btn.frame inView:self.view animated:YES];
    
    if(isValid)
    {
        docController.delegate = self;
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Sorry" message:@"You need iBooks to view this file" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Install", nil];
        [alert show];
    }
}

#pragma mark - UIAlertview Delegate Method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/in/app/ibooks/id364709193?mt=8"]]];
}


#pragma mark - UIDocumentInteractionController Delegate
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application
{
    docController = nil;
}

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller
{
    docController = nil;

}

No comments: