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. thanks

 @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);

}

Related posts

Recent Viewed