17

I would like to attach pdf created as an email attachment. I used following tutorial to create pdf on iOS device.

The downloaded pdf can be viewed at this path: /Users/”Username”/Library/Application Support/iPhone Simulator/”Your App Directory”.

I have not tried running this on ios device but I need to attach it as an email.

Link for tutorial is : http://www.ioslearner.com/generate-pdf-programmatically-iphoneipad/

Any suggestion.

user1140780
  • 988
  • 2
  • 13
  • 29
  • I dunno @PengOne. It looks like a declaration of achievement ("I created a PDF!"). Maybe he wants somebody to tell him how to use `MFMessageComposeViewController`?? – Michael Dautermann Jan 13 '12 at 05:31
  • 2
    I suggest reading the documentation and trying to implement it yourself before you ask a question. Here are the docs:http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html Try it, if you have errors come back and tell us what the errors are and then we can try to help you. – sosborn Jan 13 '12 at 05:31

2 Answers2

54

Create a MFMailComposeViewController and call addAttachmentData:mimeType:fileName:. The data will be the PDF you created. The mimeType will be application/pdf. And the fileName will be the name of the file in the email attachment. The code might look like something below:

From the tutorial you'll need to render your PDF into a NSMutableData object:

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

Then at some point in the future you'll need to pass that pdfData to the MFMailComposeViewController.

MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setSubject:@"my pdf"];
[vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SomeFile.pdf"];
Evan
  • 6,151
  • 1
  • 26
  • 43
  • Thanks Evan for the code snippet. – user1140780 Jan 13 '12 at 07:19
  • I am still lacking Voteup reputation by 2 points. :( ... Anyways, My code is working now. Thanks a ton :D – user1140780 Jan 13 '12 at 07:39
  • How to set properties to add security so that no one can edit and also print/copy the attachment. I am trying something like this: // this one is for copying CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanFalse); I looked in the apple's document but can't find the method for protection for editing. http://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html Even my above two methods are not working. – user1140780 Jan 13 '12 at 21:21
  • Here is d correct code snippet: CFMutableDictionaryRef myDictionary = NULL; myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanFalse); – user1140780 Jan 13 '12 at 21:28
  • Hi guys, when the email composer modal view appears, it is VERY SLOW because it's previewing the pdf file. Any way to have it NOT preview the pdf file? – Van Du Tran Jul 26 '13 at 15:43
2

See the docs for MFMailComposeViewController. Specifically, you're looking for the addAttachmentData:mimeType:fileName: method. That should get you going.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59
  • I think I was not clear about my question. sorry abt that. I am not sure about the path to attach pdf to email. In the sample code on developers forum: NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"]; NSData *myData = [NSData dataWithContentsOfFile:path]; Here the full path was not required as the attachment was added to the project itself. Now when i am creating pdf on the device itself, How should I give path for sending? – user1140780 Jan 13 '12 at 06:08
  • Since that tutorial is saving the PDF to the file system, you just pass in the same path to `dataWithContentsOfFile:` that you passed into `generatePdfWithFilePath:`. Make sense? – Jesse Bunch Jan 13 '12 at 06:16
  • How to set properties to add security so that no one can edit and also print/copy the attachment. I am trying something like this: // this one is for copying CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanFalse); I looked in the apple's document but can't find the method for protection for editing. http://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html Even my above two methods are not working. – user1140780 Jan 13 '12 at 21:21
  • CFMutableDictionaryRef myDictionary = NULL; myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse); CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanFalse); – user1140780 Jan 13 '12 at 21:28
  • You'll need to start a new question for that issue. – Jesse Bunch Jan 14 '12 at 18:01
  • Alright. I asked as a new thread. Thanks. – user1140780 Jan 15 '12 at 03:58