小编tek*_*tom的帖子

为什么来自WebView的dataWithPDFInsideRect不能在Mavericks上创建高质量的PDF?

运行示例项目并观察名为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)

上面的代码做了什么?

  1. WebView用于加载使用img标记嵌入PDF的HTML .在这里查看html源代码

  2. 加载webview后,尝试使用NSView的dataWithPDFInsideRect方法从文档视图中获取PDF数据

  3. 内存中的PDF数据写入磁盘

结果

  • 小牛队之前的版本(即10.8和10.7)上,上面的代码片段生成了一个高质量且内容可读的PDF文档.在预览应用程序中打开输出PDF时,可以看到高质量的PDF内容,并且在缩放时可以在不影响图像质量的情况下进行缩放.请在此处查看Mountain Lion输出PDF

  • 在Mavericks(10.9)上,上面的代码片段生成一个PDF文档,其内容模糊,图像质量下降请参阅Mavericks输出PDF

问题 …

html pdf macos cocoa webview

5
推荐指数
1
解决办法
877
查看次数

标签 统计

cocoa ×1

html ×1

macos ×1

pdf ×1

webview ×1