0

我正在从 html 生成 pdf 并附加到电子邮件。我遇到的问题是 pdf 在完成生成之前附加到电子邮件中。我尝试使用完成块但没有成功。

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

}
4

0 回答 0