pdf截图附件邮件

时间:2013-10-12 09:38:15

标签: ios pdf screenshot email-attachments

我将截屏图像从ipad转换为pdf时遇到了一些问题,并将其附加到电子邮件中。我无法理解问题出在哪里。

有人可以帮帮我吗?非常感谢您的帮助。

我的调试区域:

  

2013-10-14 12:28:20.056 emailallegato [1309:60b]   documentDirectoryFileName:   /var/mobile/Applications/F4BCEBFD-3684-45A4-8D78-D88ECA5E44F4/Documents/Test.pdf

这是我的代码:

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{

NSMutableData *pdfData = [NSMutableData data];

UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();

[aView.layer renderInContext:pdfContext];

UIGraphicsEndPDFContext();

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
}

- (IBAction)iniva:(id)sender
{

[self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Test.pdf"];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];

MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;

 if([MFMailComposeViewController canSendMail]){

    [mail setToRecipients:[NSArray arrayWithObjects:@"pdftest@gmail.com", nil]];
    [mail setSubject:@"Oggetto "];
    [mail setMessageBody:@"Messaggio e-mail" isHTML:NO];
    [mail addAttachmentData:myData mimeType:@"application/pdf" fileName:@"Material Details.pdf"];


    [self presentViewController:mail animated:YES completion:NULL];
 }}

1 个答案:

答案 0 :(得分:1)

NSString *fileName1;

fileName1 = [NSString stringWithFormat:@"test.pdf"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *dataFilePath = [saveDirectory stringByAppendingPathComponent:fileName1];

NSData *pdfData = [NSData dataWithContentsOfFile:dataFilePath];

然后附上此数据

这些是pdf创建的有用链接:

https://github.com/bygreencn/iPhonePDF

http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/

https://github.com/KurtCode/PDFKitten

http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/