Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2011, 03:32 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Talking Need help adding a pdf generated within my application as an attachment to an email

Hello,

I am looking for help on adding a pdf generated by my app to an in-app email using MFMailComposer. I think I have the pdf part working (but I am running it on my phone and have not coded the viewing of it yet as I don't need that), and I have the email portion working. The problem I am having is the attachment. The [picker addAttachmentData:NSObject mimeType:@"pdf" fileName:??]; is my problem. The pdf is created as a UIImage, but the addAttachmentData wants an NSObject with a path and filename that I don't have. I think I just need to write that UIImage to a path and filename that I can access as an object afterwards. PLEASE HELP.
Karl6669 is offline   Reply With Quote
Old 06-26-2011, 06:32 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

addAttachmentData needs an NSData pointer, not just an NSObject pointer; use NSData's dataWithContentsOfFile to supply this data.

I'm confused, though when you are talking about UIImages. Is it a UIImage, or is it a PDF? If it's a UIImage, you should be adding it as a png or a jpeg.
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-26-2011, 07:10 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
addAttachmentData needs an NSData pointer, not just an NSObject pointer; use NSData's dataWithContentsOfFile to supply this data.

I'm confused, though when you are talking about UIImages. Is it a UIImage, or is it a PDF? If it's a UIImage, you should be adding it as a png or a jpeg.
Ya, I was actually looking at the wrong chunk of code when I wrote this. I was looking at a section where I take a screenshot of whatever's on my screen at the moment, and then draw that image into the pdf file. It obviously does return a pdf file, you are correct.

So, I already had an NSData dataWithContentsOfFile in there, but, it isn't working. How do I retrieve the path and filename of the pdf? Here is my code on this (I threw some NSLogs in there and an NSString pointer (attachmentFileName) and an NSData pointer (attachmentPDF) so I could try some different things out, but, all were un-succsessful.


void CreatePDFFile (CGRect pageRect, const char *filename) {

// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);
// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for 'signing' the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("DOORMASTER PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it's time to draw to it

// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);

// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));

// This code block will create an image that we then draw to the page
/*
const char *picture = "Picture";
CGImageRef image;
CGDataProviderRef provider;
CFStringRef picturePath;
CFURLRef pictureURL;

picturePath = CFStringCreateWithCString (NULL, picture,
kCFStringEncodingUTF8);
pictureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), picturePath, CFSTR("png"), NULL);
CFRelease(picturePath);
provider = CGDataProviderCreateWithURL (pictureURL);
//CFRelease(pictureURL);
image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease (provider);
CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385),image);
CGImageRelease (image);
// End image code
*/

// Adding some text on top of the image we just added
CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *text = "Hello World!";
CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));
// End text

// We are done drawing to this page, let's end it
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
CGContextEndPage (pdfContext);

// We are done with our context now, so we release it
CGContextRelease (pdfContext);

}

-(IBAction)sendReportid)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"myPDF.pdf";
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CreatePDFFile(CGRectMake(0, 0, 612, 792),filename);
NSString *path = [[NSBundle mainBundle] pathForResource:newFilePath ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFileath];
attachmentPDF = myData;
NSLog(@"Attachment Name: %@", attachmentPDF);
attachmentFileName = [path stringByStandardizingPath];
NSLog(@"Attachment File Name: %@", attachmentFileName);
[self checkCanSendEmail];
}
Karl6669 is offline   Reply With Quote
Old 06-26-2011, 07:23 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

At a glance, instead of this:

Code:
NSString *path = [[NSBundle mainBundle] pathForResource:newFilePath ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
looks like you should be doing this:

Code:
NSData *myData = [NSData dataWithContentsOfFile:newFilePath];
Tip: Use CODE tags around code. Also, this post probably would have gotten earlier responses if you'd posted it in the right forum (iPhone SDK Development - iPhone Dev SDK Forum).
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 06-26-2011, 07:52 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 44
Karl6669 is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
At a glance, instead of this:

Code:
NSString *path = [[NSBundle mainBundle] pathForResource:newFilePath ofType:@"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
looks like you should be doing this:

Code:
NSData *myData = [NSData dataWithContentsOfFile:newFilePath];
Tip: Use CODE tags around code. Also, this post probably would have gotten earlier responses if you'd posted it in the right forum (iPhone SDK Development - iPhone Dev SDK Forum).
[quote]
Thanks, that worked. I had to make a few other minor adjustments, but, tested and working. Thank you thank you thank you.
Karl6669 is offline   Reply With Quote
Reply

Bookmarks

Tags
attach, email, pdf

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
11 members and 392 guests
AppleDev, chemistry, Emy, Gi-lo, ipodphone, mistergreen2011, pipposanta, QuantumDoja, Retouchable, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,129
Posts: 402,924
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0