2012-08-25 43 views
0

我在過去的幾個小時裏一直在努力解決這個問題。我以編程方式製作pdf並將其保存在〜/ library/Application Support/iPhone Simulator/...上,並且這些程序在模擬器中可以正常工作。我可以在webView中保存和檢索pdf。pdf在模擬器中顯示,但不在iPhone中

但是,當我在iPhone4設備上測試我的應用程序時,我沒有看到PDF文件,當我檢索。我是新手,所以我不知道手動檢查設備中是否創建了pdf。

請給我幫助。

感謝

我保存PDF從這個代碼:

- (void)generatePdfButtonPressed{ 

pageSize = CGSizeMake(612, 792); 
NSString *fileName = @"Demo.pdf"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 

[self generatePdfWithFilePath:pdfFileName]; 
} 


- (void) generatePdfWithFilePath: (NSString *)thefilePath 
{ 
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 

NSInteger currentPage = 0; 
BOOL done = NO; 
do 
{ 
    // Mark the beginning of a new page. 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

    // Draw a page number at the bottom of each page. 
    currentPage++; 
    //[self drawPageNumber:currentPage]; 

    //Draw a border for each page. 
    // [self drawBorder]; 

    //Draw text fo our header. 
    [self drawHeader]; 

    //Draw a line below the header. 
    [self drawLine]; 


    //Draw personel details 
    [self drawPersonelDetail]; 

    //Draw Education 
    [self drawEducation]; 




    //Draw work Experiences 
    [self drawWorkExperiences]; 

    //Draw work Activities 
    [self drawActivities]; 


    //Draw Skills 
    [self drawSkils]; 


    //Draw some text for the page. 
// [self drawText]; 

    yCord=550; 
// [self drawText]; 

    //Draw an image 
// [self drawImage]; 
    done = YES; 
} 
while (!done); 

// Close the PDF context and write the contents out. 
UIGraphicsEndPDFContext(); 
} 
+1

無處不在添加日誌消息,並檢查並記錄返回代碼。很明顯,你在電話上做的事情不起作用,這就是你如何找到那個地方。另外,我不確定手機應用程序中是否存在「應用程序支持」 - 您可能需要以編程方式創建它(如我所說,我不確定) –

+0

@DavidH感謝您的回覆。我想知道。我正在做正確的編碼在我的設備中存儲PDF? (它在模擬器中工作正常) – QueueOverFlow

+0

另外,你沒有顯示的是你如何存儲你已經生成的文件路徑,以及如何試圖將PDF加載到Web視圖。 –

回答

2

經過漫長的努力,我找到了自己的問題的解決方案:我在下面提供解決方案,可能會幫助其他任何人。

- (void)generatePdfButtonPressed{ 

pageSize = CGSizeMake(612, 792); 
NSString *fileName = @"Demo.pdf"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 

[self generatePdfWithFilePath:pdfFileName]; 
} 


- (void) generatePdfWithFilePath: (NSString *)thefilePath 
{ 
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 

NSInteger currentPage = 0; 
BOOL done = NO; 
do 
{ 
// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

// Draw a page number at the bottom of each page. 
currentPage++; 
[self drawPageNumber:currentPage]; 

//Draw a border for each page. 
[self drawBorder]; 

//Draw text our header. 
[self drawHeader]; 

//Draw a line below the header. 
[self drawLine]; 


//Draw personel details 
[self drawPersonelDetail]; 

//Draw Education 
[self drawEducation]; 


//Draw an image 
[self drawImage]; 

done = YES; 
} 
while (!done); 

// Close the PDF context and write the contents out. 
UIGraphicsEndPDFContext(); 
} 



And for Display the pdf file the method is : 

- (void)showPdfClicked 
{ 

pdf1View.scalesPageToFit = YES; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSLog(@"%@",documentsDirectory); 

NSString *path1 =[documentsDirectory stringByAppendingPathComponent:@"Demo.pdf"]; 

NSLog(@"Retruve Save path is %@: ",path1); 
// NSString *path1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"]; 
NSURL *targetURL1 = [NSURL fileURLWithPath:path1]; 
NSURLRequest *request1 = [NSURLRequest requestWithURL:targetURL1]; 
[pdf1View loadRequest:request1]; 

} 
+0

那麼,這段代碼與原文有什麼不同?什麼解決了你的問題? –

+0

@SixtenOtto我在做錯誤之前,當我創建PDF時,我給出了名字「Demo.pdf」,但是當我訪問pdf文件時我使用了「demo.pdf」,所以它不顯示。 – QueueOverFlow

0

我以前碰到這個問題。我的猜測是這是一個內存問題 - 您嘗試加載的PDF文件有多大? UIWebView似乎有一些問題緩衝(如果它這樣做)大型文件顯示。

0

我成功地通過電子郵件通過此代碼發送了我創建的pdf。可能會幫助你(dottorfeelgood)。

-(IBAction)emailButtonClicked 
{ 


MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
picker.mailComposeDelegate = self; 
[picker setSubject:@"Subject"]; 


NSString *fileName = @"Demo.pdf"; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 



NSMutableData *pdfData = [NSMutableData data]; 


pdfData = [NSData dataWithContentsOfFile:pdfFileName]; 

CGRect bounds=CGRectMake(0, 0, 612, 792); 
[picker addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SomeFile.pdf"]; 




// Fill out the email body text 
NSString *emailBody = @"Image is attached"; 
[picker setMessageBody:emailBody isHTML:NO]; 
[self presentModalViewController:picker animated:YES]; 

//[picker release]; 


} 

快樂編碼!

相關問題