如何在iPhone sdk中将NSData转换为pdf? [英] How to convert a NSData to pdf in iPhone sdk?

查看:141
本文介绍了如何在iPhone sdk中将NSData转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将网页转换为pdf文件。我做了以下,

Am converting a webpage as a pdf file. I did the following,

NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray objectAtIndex:pageIndex]];
[controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string];
[self presentModalViewController:controller1 animated:YES];
[controller1 release];

现在我如何将我的NSData转换为pdf并保存在我的应用程序内存中?请帮我提供示例代码或建议。谢谢大家。

Now how can i convert my NSData into pdf and save in my application memory? Kindly help me with sample codes or suggestions. Thanks all.

推荐答案

假设您在此处的文档目录中有pdf。您可以将其更改为实际位置。试试这个 -

Am assuming you have your pdf in documents directory here. You can change it to where ever it actually is. Try this -

//to convert pdf to NSData
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"]; 
NSData *myData = [NSData dataWithContentsOfFile:pdfPath]; 

基本上使用 CGPDFDocumentCreateWithProvider 你可以转换 NSData 到pdf,

Essentially using CGPDFDocumentCreateWithProvider you can convert NSData to pdf,

//to convert NSData to pdf
NSData *data               = //some nsdata
CFDataRef myPDFData        = (CFDataRef)data;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf       = CGPDFDocumentCreateWithProvider(provider);

不要忘记 CFRelease 所有未使用的完成后的数据。

Don't forget to CFRelease all unused data after you are done.

这篇关于如何在iPhone sdk中将NSData转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆