How to attached pdf to email

2019-09-02 13:53发布

I am doing a app for Ipad,I am trying to attached a pdf file to send by email ,for this I create a pdf like this.

pageSize= CGSizeMake(612, 792);
    NSString *fileName=@"prueba.pdf";
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];

    NSString *url=[fileName stringByAppendingPathComponent:fileName];
    [self generatePdfWithFilePath:pdfFileName];
    NSData *pdfData = [NSData dataWithContentsOfFile:url];

the next step is create a email to send the pdf I dis this, like this..

   if ([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"esto es para probar"];
        NSArray *toRecipients = [NSArray arrayWithObjects:currentuser.email, nil];
        [mailer setToRecipients:toRecipients];
        [mailer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:fileName];
        NSString *emailBody = @"correo prueba";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        mailer.modalPresentationStyle = UIModalPresentationPageSheet;

    }else{
...

I generate I create the pdf and everything is right, when I see the email I see de pdf attached like a image (picture), but the problem is that when I receive the email nothing is attached.

any idea

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-02 14:08

It seems to me that in your code you are reversing the order of the two following steps:

NSData *pdfData = [NSData dataWithContentsOfFile:url];
[self generatePdfWithFilePath:pdfFileName];

If I interpret correctly the meaning of generatePdfWithFilePath:pdfFileName, I assume this step should be executed before the other. I.e.:

[self generatePdfWithFilePath:pdfFileName];
NSData *pdfData = [NSData dataWithContentsOfFile:url];

Hope this helps.

查看更多
3楼-- · 2019-09-02 14:20

Finally work, I put the solution for someone.. thanks

 pageSize= CGSizeMake(612, 792);
    NSString *fileName=@"prueba.pdf";
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];
    [self generatePdfWithFilePath:pdfFileName];
    NSData *pdfData = [NSData dataWithContentsOfFile:pdfFileName];
    G7DatabaseInterface *databaseInterface = [[DatabaseInterface alloc] init];
    G7User *currentuser=[databaseInterface userByUsername:self.appDelegate.userName];

ok I explain better, sorry, before to fix it up I have this, the problem was in this line

NSString *url=[fileName stringByAppendingPathComponent:fileName];

my url was prueba.pdf/prueba.pdf so the path was wrong!!

I fix it up like this

NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];

and erase the *url I don't use it.

查看更多
登录 后发表回答