运行示例项目并观察名为SaveWebPage.pdf的桌面上生成的输出PDF
粘贴感兴趣的代码片段
NSURL *url = [[NSBundle mainBundle] URLForResource:@"Page" withExtension:@"html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[[self.webview mainFrame] loadRequest:req];
NSView *view = [[[self.webview mainFrame] frameView] documentView];
NSData *data = [view dataWithPDFInsideRect:[view bounds]];
NSArray* deskTopArrayPaths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString* deskTopPath = [deskTopArrayPaths objectAtIndex:0];
NSString* pdfPath = [NSString stringWithFormat:@"%@/SaveWebPage.pdf",deskTopPath];
PDFDocument *doc = [[PDFDocument alloc] initWithData:data];
[doc writeToFile:pdfPath];
Run Code Online (Sandbox Code Playgroud)
上面的代码做了什么?
WebView用于加载使用img标记嵌入PDF的HTML .在这里查看html源代码
加载webview后,尝试使用NSView的dataWithPDFInsideRect方法从文档视图中获取PDF数据
内存中的PDF数据写入磁盘
结果
在小牛队之前的版本(即10.8和10.7)上,上面的代码片段生成了一个高质量且内容可读的PDF文档.在预览应用程序中打开输出PDF时,可以看到高质量的PDF内容,并且在缩放时可以在不影响图像质量的情况下进行缩放.请在此处查看Mountain Lion输出PDF
在Mavericks(10.9)上,上面的代码片段生成一个PDF文档,其内容模糊,图像质量下降请参阅Mavericks输出PDF
问题 …