从 UIWebView 或 UIView 获取 PDF/PNG 作为输出 [英] Get a PDF/PNG as output from a UIWebView or UIView

查看:18
本文介绍了从 UIWebView 或 UIView 获取 PDF/PNG 作为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以获取 UIWebView 的内容并将其转换为 PDF 或 PNG 文件?例如,我想通过在从 Safari 打印时选择 PDF 按钮来获得与 Mac 上可用的输出类似的输出.我假设这是不可能的/内置的,但希望我会感到惊讶并找到一种方法将内容从 webview 获取到文件.

Is there any way to get the content of a UIWebView and convert it to a PDF or PNG file? I'd like to get similar output to that available on the Mac by selecting the PDF button when printing from Safari, for example. I'm assuming this isn't possible/built in yet, but hopefully I'll be surprised and find a way to get the content from a webview to a file.

谢谢!

推荐答案

您可以在 UIView 上使用以下类别创建 PDF 文件:

You can use the following category on UIView to create a PDF file:

#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 中.

Bad news: UIWebView does not create nice shapes and text in the PDF, but renders itself as an image into the PDF.

这篇关于从 UIWebView 或 UIView 获取 PDF/PNG 作为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆