使用Cocoa打印本地HTML [英] Print Local HTML using Cocoa

查看:146
本文介绍了使用Cocoa打印本地HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的项目列表,需要使用Cocoa打印。我有一个半烘焙的解决方案,使用NSView与自定义 drawRect:方法,但它是相当复杂,不容易维护。

I have a simple list of items that needs to be printed using Cocoa. I have a half-baked solution that uses an NSView with a custom drawRect: method, but it's fairly complex and not very easy to maintain.

我想要的是一个HTML字符串(可以从列表中轻松构造),可以嵌入一个一次性WebView,然后打印。

What I would like to have is an HTML string (which could be easily constructed from the list) that can be embedded in a one-off WebView, then printed.

假设我有一个简单的NSString,例如:

Assuming I have a simple NSString like:

NSString *htmlString = @"<b>Test</b>";

创建显示此内容的WebView最简单的方法是什么?我试过下面的代码,但它导致一个单一的空白页:

What's the easiest method for creating a WebView displaying this content? I've tried the below code, but it results in a single blank page:

WebView *webView = [[WebView alloc] init];
NSString *dir = @"/Users/Me/Desktop/";
NSString *fileUrl = [dir stringByAppendingPathComponent:@"Temp_Print.html"];

NSString *htmlString = @"<b>Hi!</b>";
[[htmlString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileUrl atomically:YES];

[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileUrl]]];
[webView setFrame:NSMakeRect(0, 0, 500, 500)];
NSPrintOperation *po = [NSPrintOperation printOperationWithView:webView printInfo:pi];

[pi release];
[po runOperation];


推荐答案

运行循环需要迭代才能实际加载内容。我只是完成运行框架加载委托方法中的实际打印操作:

The run loop needs to iterate in order for the content to actually load. I simply finished running the actual print operation in the frame load delegate method:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
   ...
}

a href =http://lists.apple.com/archives/webkitsdk-dev/2008/Oct/msg00018.html =nofollow>来源

Source

这篇关于使用Cocoa打印本地HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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