我有图片或任何文字。我想创建一个像test.pdf
这样的PDF文件。
创建pdf文件后,我想用MailComposer
邮寄该文件。
请有人建议如何在ios编码中创建.pdf
文件?
答案 0 :(得分:1)
在你的pdf创建按钮点击动作中写下这段代码..
#import "PDF_Screen.h"
// Create Pdf...
PDF_Screen *loPdf = [[PDF_Screen alloc] init]; // this is class for create pdf file
[loPdf createPDF:@"Report.Pdf"];
// Attach with mail ......
Mail_Screen *lomailscreen = [[Mail_Screen alloc] initWithNibName:nil bundle:nil];
lomailscreen.mToRecepient = @"";
lomailscreen.mSubject = @"Budget Report";
lomailscreen.mBody = @"";
[self.navigationController pushViewController:lomailscreen animated:YES];
这里我要编写pdf类的代码..
<强> PDF_Screen.h 强>
#import <Foundation/Foundation.h>
@interface PDF_Screen : NSObject
{
}
void CreatePDFFile (CGRect pageRect, const char *filename);
- (void)createPDF:(NSString *)PdfFilename;
@end
<强> PDF_Screen.m 强>
#import "PDF_Screen.h"
@implementation PDF_Screen
-(id)init
{
self = [super init];
if (self)
{
}
return self ;
}
-(void)createPDF:(NSString *)PdfFilename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = PdfFilename ;
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CreatePDFFile(CGRectMake(0, 0, 320, 345), filename);
}
void CreatePDFFile (CGRect pageRect , const char *filename)
{
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("My 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 20 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(20, 20, pageRect.size.width - 40, pageRect.size.height - 40));
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @"Picture.jpeg" ;
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
UIImage *newUIImage1 =[UIImage imageWithContentsOfFile:newFilePath];
NSData * data1 = UIImageJPEGRepresentation(newUIImage1, 0.95);
UIImage * compressedImage1 = [[UIImage alloc] initWithData:data1];
CGImageRef image1 = [compressedImage1 CGImage];
CGContextDrawImage (pdfContext, CGRectMake(21, 21, 278, 303),image1);
// 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);
}
-(void)dealloc
{
[super dealloc];
}
@end
我认为这会帮助你...... :)