0

我在将屏幕截图从 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];
 }}
4

1 回答 1

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/

于 2013-10-12T09:48:16.960 回答