2013-05-29 36 views
5

嗨我想用NSData或使用webservice給出的數據字節來編寫pdf嗎?如何使用NSData或使用數據字節目標c生成pdf?

-(NSData *)saveData:(NSData*)fileData fileName:(NSString *)fileName fileType:(NSString *)fileType 
{ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    NSString *filePath = [NSString stringWithFormat:@"%@/%@.%@",documentsDir,fileName,fileType]; 

    NSData* downloadData = [NSData dataWithData:fileData]; 

    [fileManager createFileAtPath:filePath contents:downloadData attributes:nil]; 


} 

我使用這個,但它不工作它給我錯誤「它可能會被損壞或使用預覽不識別的文件格式。」打開上面的代碼創建的pdf。

+0

這是一個更好的主意只使用'NSData'的'writeToFile:atomically:'比通過NSFileManager。 – CodaFi

+0

數據是否已經以PDF格式存在,或者您在尋找將其他數據轉換爲PDF的方式?你可以嘗試使用十六進制編輯器檢查你的文件([Hex Fiend](http://ridiculousfish.com/hexfiend/)真棒)。您很可能會想出數據的格式。 –

+0

您也可以使用'file'命令行工具來找出某個文件的實際類型。 –

回答

7

你可以轉換到NSDataPDF波紋管代碼...我得到的婁代碼從This link

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

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

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

-(IBAction)saveasPDF:(id)sender{ 
    NSString *string=[NSString stringWithFormat:@"%@.pdf",[yourArray objectAtIndex:pageIndex]]; 
    [controller1 addAttachmentData:pdfData mimeType:@"application/pdf" fileName:string]; 
    [self presentModalViewController:controller1 animated:YES]; 
    [pdfData writeToFile:[self getDBPathPDf:string] atomically:YES]; 
} 

-(NSString *) getDBPathPDf:(NSString *)PdfName { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
     NSString *documentsDir = [paths objectAtIndex:0]; 
     return [documentsDir stringByAppendingPathComponent:PdfName]; 
} 

摘要:從PDF文件路徑 得到的NSData

NSData *pdfData = [NSData dataWithContentsOfFile:pathToFile1]; 
NSLog(@"pdfData= %d", pdfData != nil); 

將NSData寫入pdf文件

[pdfData writeToFile:pathToFile2 atomically:YES]; 
+1

謝謝你工作得很好:) –

+0

@ParasJoshi,我嘗試使用「writeToFile:」保存文件,但它不打開Preview.app中的文件。任何幫助如何將上面的CGPDFDocumentRef對象保存爲pdf文件 – Meet

+1

有沒有人有這樣的Swift 2? – ChallengerGuy

1

@ChallengerGuy(如果你還在尋找的CGPDFDocument雨燕2方法)

//to convert data of type NSData 

    guard let cfData = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length) else { return nil} 

    let cgDataProvider = CGDataProviderCreateWithCFData(cfData) 

    guard let cgPDFDocument = CGPDFDocumentCreateWithProvider(cgDataProvider) else { return nil } 

希望以上有助於

相關問題