2011-11-04 41 views
0

正在將網頁轉換爲pdf文件。我做了以下,如何在iPhone SDK中將NSData轉換爲pdf?

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並保存在我的應用程序內存?請幫助我提供示例代碼或建議。謝謝大家。

+0

哪裏pdfData從何而來? – Sven

+0

對不起,我讓你困惑。我想將NSData轉換爲pdf並存儲在我的應用程序內存中。 – Saranya

+4

這是什麼「存儲在我的應用程序內存」是什麼意思?一個NSData對象已經在內存中。另外,「轉換爲PDF」沒有解釋你轉換的格式並不意味着什麼。 – benzado

回答

14

假設你在這裏的文檔目錄中有你的pdf。您可以將其更改爲實際所處的位置。試試這個 -

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

基本採用CGPDFDocumentCreateWithProvider你可以轉換NSData爲PDF格式,

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

不要忘了CFRelease你完成所有未使用的數據之後。

+0

對不起,我讓你困惑。我想將NSData轉換爲pdf並存儲在我的應用程序內存中。 – Saranya

+1

查看我的更新回答 –

+0

嗨,我得到了答案,謝謝。 – Saranya

3

我這是一個簡單的方法如下,

-(IBAction)saveasPDF:(id)sender{ 
    NSString *string=[NSString stringWithFormat:@"%@.pdf",[chapersArray 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]; 
} 
1

您可以使用此解決方案嘗試:

 
- (void)saveDataToPDF:(NSData *)pdfDocumentData 
{ 
    //Create the pdf document reference 
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData); 
    CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider); 

    //Create the pdf context 
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0); 

    //NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height); 
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData); 
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL); 


    if (CGPDFDocumentGetNumberOfPages(document) > 0) 
    { 
     //Draw the page onto the new context 
     //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 

     CGPDFContextBeginPage(pdfContext, NULL); 
     CGContextDrawPDFPage(pdfContext, page); 
     CGPDFContextEndPage(pdfContext); 
    } 
    else 
    { 
     NSLog(@"Failed to create the document"); 
    } 

    CGContextRelease(pdfContext); //Release before writing data to disk. 

    //Write to disk 
    [(__bridge NSData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES]; 

    //Clean up 
    CGDataProviderRelease(dataProvider); //Release the data provider 
    CGDataConsumerRelease(dataConsumer); 
    CGPDFDocumentRelease(document); 
    CFRelease(mutableData); 
}