2010-03-16 127 views
16

有什麼辦法可以獲得UIWebView的內容並將其轉換爲PDF或PNG文件?例如,我想通過從Safari打印時選擇PDF按鈕來獲得與Mac上可用的輸出類似的輸出。我假設這是不可能的/內置的,但希望我會感到驚訝,並找到一種方法來從webview的內容到文件。從UIWebView或UIView獲取PDF/PNG輸出

謝謝!

回答

21

您可以使用的UIView以下類別來創建一個PDF文件:

#import <QuartzCore/QuartzCore.h> 

@implementation UIView(PDFWritingAdditions) 

- (void)renderInPDFFile:(NSString*)path 
{ 
    CGRect mediaBox = self.bounds; 
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], &mediaBox, NULL); 

    CGPDFContextBeginPage(ctx, NULL); 
    CGContextScaleCTM(ctx, 1, -1); 
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height); 
    [self.layer renderInContext:ctx]; 
    CGPDFContextEndPage(ctx); 
    CFRelease(ctx); 
} 

@end 

壞消息:UIWebView中不會產生很好的形狀和在PDF文本,但呈現本身作爲一個圖像到PDF。

+0

感謝您的帖子。雖然CGPDFContextCreateWithURL沒有正確創建上下文,但我遇到了一個問題。我試過幾個不同的網址,它似乎沒有改變任何東西。有任何想法嗎? – mjdth 2010-03-23 21:04:32

+0

CFURLRef是如何創建的。我修復了它,它現在應該適用於正確的文件URL。 – 2010-08-15 09:50:01

+0

使用此方法呈現UILabels似乎可以柵格化文本。如果你想讓你的文本保持向量化,直接將它們繪製到圖層中,如此處所述http://stackoverflow.com/questions/2209734/add-text-to-calayer – 2011-10-02 19:45:45

5

創建從Web視圖中的圖像很簡單:

UIImage* image = nil; 

UIGraphicsBeginImageContext(offscreenWebView_.frame.size); 
{ 
    [offscreenWebView_.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
} 
UIGraphicsEndImageContext(); 

一旦你的形象,你可以將其保存爲PNG。

也可以以非常類似的方式創建PDF,但僅限於尚未發佈的iPhone OS版本。

+0

我認爲CGPDF ... API是從iPhone OS 2.0開始的。 – 2010-03-16 14:00:31

+0

是的,但我認爲渲染一個圖層到PDF上下文是一個新的東西。 – 2010-03-16 14:31:38

+0

這個答案也很有效。問題是頁面之間頁面變化的大小。有什麼方法可以確定webview中內容的大小,以便我可以創建正確大小的上下文嗎? – mjdth 2010-03-23 23:21:34

0

@mjdth,請嘗試fileURLWithPath:isDirectory:URLWithString也不適合我。

@implementation UIView(PDFWritingAdditions) 

- (void)renderInPDFFile:(NSString*)path 
{ 
    CGRect mediaBox = self.bounds; 
    CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path isDirectory:NO], &mediaBox, NULL); 

    CGPDFContextBeginPage(ctx, NULL); 
    CGContextScaleCTM(ctx, 1, -1); 
    CGContextTranslateCTM(ctx, 0, -mediaBox.size.height); 
    [self.layer renderInContext:ctx]; 
    CGPDFContextEndPage(ctx); 
    CFRelease(ctx); 
} 

@end 
0

下面的代碼會將UIWebView的(完整)內容轉換爲UIImage。

渲染UIImage後,我將它作爲PNG寫入磁盤以查看結果。
當然,你可以用UIImage做任何你喜歡的事情。

UIImage *image = nil; 
CGRect oldFrame = webView.frame; 

// Resize the UIWebView, contentSize could be > visible size 
[webView sizeToFit]; 
CGSize fullSize = webView.scrollView.contentSize; 

// Render the layer content onto the image 
UIGraphicsBeginImageContext(fullSize); 
CGContextRef resizedContext = UIGraphicsGetCurrentContext(); 
[webView.layer renderInContext:resizedContext]; 
image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

// Revert the UIWebView back to its old size 
webView.frame = oldFrame; 

// Write the UIImage to disk as PNG so that we can see the result 
NSString *path= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"]; 
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES]; 

注意:確保UIWebView已完全加載(UIWebViewDelegate或加載屬性)。