2010-03-16 122 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或加载属性)。