0

I am generating a pdf from html and attaching to email. The problem I have is the pdf is attaching to the email before it has finished generating. I have tried using a completion block but without success.

 @property (nonatomic, strong) void(^completionHandler)(BOOL);
[self generatePDF:html andCompletionHandler:^(BOOL result) {            
// COMPLETED ? - ATTACH the file  

                    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                    NSString *pdfPathFile=[docDir stringByAppendingString:@"/report.pdf"];
                    NSData *pdfData = [NSData dataWithContentsOfFile:pdfPathFile];
                    [controller setMessageBody:@"report attached" isHTML:NO];
                    [controller addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"report.pdf"];
                    NSLog(@"attaching pdf with length %d",[pdfData length]);

                }];
-(void)generatePDF:(NSMutableString*)HtmlString andCompletionHandler:(void (^)(BOOL result))handler{

_completionHandler = handler;

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];


NSString *pdfPathFile=[docDir stringByAppendingString:@"/report.pdf"];

NSFileManager *fm = [NSFileManager defaultManager];

NSError *error;

if ([fm fileExistsAtPath:pdfPathFile] == YES){
    [fm removeItemAtPath:pdfPathFile error:&error];
}
//I am using NDHTMLtoPDF to create the PDF

 self.PDFCreator = [NDHTMLtoPDF createPDFWithHTML:HtmlString pathForPDF:pdfPathFile delegate:self pageSize:kPaperSizeA4 margins:UIEdgeInsetsMake(10,5,10,5)];

}

#pragma mark NDHTMLtoPDFDelegate

- (void)HTMLtoPDFDidSucceed:(NDHTMLtoPDF*)htmlToPDF
{

NSString *result = [NSString stringWithFormat:@"HTMLtoPDF did succeed - %@", htmlToPDF.PDFpath];
NSLog(@"%@",result);

_completionHandler(YES);

}
tshepang
  • 12,111
  • 21
  • 91
  • 136
paulco
  • 23
  • 3
  • What specifically is failing with this code? – CrimsonChris Sep 08 '14 at 01:35
  • 2
    I don't really see why you're using a block here. You should just have the didSucceed delegate method either start the mail composition itself or have it call another method that does that. – Joshua Sullivan Sep 08 '14 at 02:11
  • Can you please show us the code where you present the `MFMailComposeViewController` instance? I think, you have some buggy lines of code there. – Fahri Azimov Sep 08 '14 at 04:44
  • if ([MFMailComposeViewController canSendMail]) { // set the sendTo address NSMutableArray *recipients = [[NSMutableArray alloc] initWithCapacity:1]; [recipients addObject:email]; MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; [controller setSubject:subject]; [controller setToRecipients:recipients]; – paulco Sep 08 '14 at 06:03
  • Specifically, The email is being displayed without the pdf attachment. – paulco Sep 08 '14 at 06:13

0 Answers0