2012-05-07 102 views
0

在我的應用程序中有一個帶有HTML內容的WebView。內容是WebView中的3頁。 現在我想轉換PDF中的WebView內容。我從WebView內容創建了3個圖像。現在我想使用這3個圖像創建pdf。每張圖片應該有一頁PDF。但它產生爲單頁圖像。所以當我正在打印時,內容被切斷。 我使用這個,將多個圖像轉換爲多頁PDF格式

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

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 

// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

double currentHeight = 0.0; 

for (int index = 1; index <= imageName ; index++) 
{ 
    NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]]; 
    UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath]; 

    [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];   
    currentHeight += pngImage.size.height; 
} 

UIGraphicsEndPDFContext(); 

我在做什麼毛病此代碼。

感謝

+0

你想有1頁上的所有內容? – mbm29414

+0

關於我唯一能看到代碼「錯誤」的地方是,頁面的初始高度(2324)***可能會從圖像高度的總和中斷開。你使用'pngImage.size.height'迭代'currentHeight',它可能不會達到2324.除此之外,使用我自己的PNG文件,你的代碼就可以工作。但就我而言,第三頁(因爲其高度爲960像素)部分被切斷。 – mbm29414

回答

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

UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);  

double currentHeight = 0.0; 

for (int index = 1; index <= imageName ; index++) 
{ 
    NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]]; 
    UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath]; 

// TODO: See Here 
// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pngImage.size.height), nil); 

    [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];   
    //currentHeight += pngImage.size.height; 
} 

UIGraphicsEndPDFContext(); 
0
/*For call method*/ 

[self createPdfWithName:@"sam" array:[NSArray arrayWithObjects:[UIImage imageNamed:@"01.png"],[UIImage imageNamed:@"02.png"], nil]]; 

/*Create New Pdf*/ 

- (NSString *)createPdfWithName: (NSString *)name array:(NSArray*)images 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docspath = [paths objectAtIndex:0]; 
    NSString *pdfFileName = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",name]]; 
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil); 
    for (int index = 0; index <[images count] ; index++) 
    { 
     UIImage *pngImage=[images objectAtIndex:index];; 
     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height)), nil); 
     [pngImage drawInRect:CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height))]; 
    } 
    UIGraphicsEndPDFContext(); 
    return pdfFileName; 
} 
相關問題