从视图生成 PDF 以发送电子邮件 [英] Generate a PDF from a view for emailing
问题描述
我是 Objective C 的新手,所以请帮帮我.
I'm super new to Objective C, so please help me out.
我有一个包含多个标签和文本视图的视图.我正在尝试为该页面/视图生成一个 PDF 文件并附加到电子邮件中.
I have a view with several labels and text views. I am trying to generate a PDF file for that page/view and attach to an email.
首先,我尝试了这种方法,但似乎文件未创建或未附加到电子邮件中.这是我的代码:
First, I tried this approach and it seems either the file is not created or not attached to the email. Here are my codes:
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
由于这不起作用,我尝试了另一种方法,先单独创建 PDF,然后将其附加到电子邮件中.然后我看到在那个目录下创建了PDF文件,但是是空的!!!
And since that didn't work, I tried a different approach by creating the PDF separately first, then attach it to the email. Then I saw that the PDF file is created at that directory, but it is empty!!!
这是我修改后的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:@"/tempFile.pdf"];
UIGraphicsBeginPDFContextToFile(file, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
- 能否请您帮助我从视图成功创建 PDF,然后将其附加到电子邮件中?
我尝试了多种方法,但无法弄清楚我做错了什么.非常感谢!!!
I have tried several ways but can't figure out what I am doing wrong. Thanks so much!!!
推荐答案
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[mailComposer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"file.pdf"];
[self presentModalViewController:mailComposer animated:YES];
注意:不知道 draw rect 适用于视图.对我来说它不起作用!不需要往文件里写数据,直接写邮件就可以了
Note : Don't know the draw rect works for views.For me its not working! No need to write data into a file.you can directly do it while the mail is composed as
[mailComposer addAttachmentData:pdfData mimeType:@"pdf" fileName:@"file.pdf"];
试试这个.mime 类型只需要字符串 pdf.还包括文件名 file.pdf 的扩展名.
try this .The mime type requires only the string pdf.Also include the extension to the file name file.pdf.
快乐编码:)
这篇关于从视图生成 PDF 以发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!