WebView(OSX)无法在打印面板预览中呈现 [英] WebView (OSX) not rendering in print panel preview

查看:126
本文介绍了WebView(OSX)无法在打印面板预览中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用创建并打印了WebView.输出页面正在正确地组装和打印,但是在打印面板"中无法获得页面的预览.

My app creates and prints a WebView. The output page is being assembled and printed properly, but I do not get a preview of the page in the Print Panel.

NSRect printViewFrame = {};
printViewFrame.size.width = paperSize.width - marginLR*2;
printViewFrame.size.height = paperSize.height - marginTB*2;
WebView *printView = [[WebView alloc] initWithFrame: printViewFrame frameName:@"printFrame" groupName:@"printGroup"];
[printView setShouldUpdateWhileOffscreen: YES];

// use the template engine to generate the document HTML

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDir = [[paths objectAtIndex:0] stringByAppendingPathComponent: [[NSProcessInfo processInfo] processName]];
NSString *templatePath = [[supportDir stringByAppendingPathComponent: @"Templates"] stringByAppendingPathComponent: @"Default.mustache"];
GRMustacheTemplate *template = [GRMustacheTemplate templateFromContentsOfFile: templatePath error: NULL];

NSString *webviewHTMLString = [template renderObject: reportDict error:NULL];

// Print it

[[printView mainFrame] loadHTMLString: webviewHTMLString baseURL: tempImageURL];
NSPrintOperation *printOp = [NSPrintOperation printOperationWithView: [[[printView mainFrame] frameView] documentView] printInfo: printInfo];
[printOp setShowsPrintPanel: YES];  
[printOp runOperation];
[printView release];

此外,如果尝试使用备用printOperation在后台打印,则会得到空白页:

In addition, if I try to print in the background using the alternate printOperation, I get a blank page:

[printOp runOperationModalForWindow: mainWindow delegate:self didRunSelector:@selector(printOperationDidRun:success:contextInfo:) contextInfo: nil];

有什么想法吗?

推荐答案

事实证明,[[printView mainFrame] loadHTMLString: webviewHTMLString baseURL: tempImageURL];在渲染完成之前不会阻塞.更改代码如下:

Turns out that [[printView mainFrame] loadHTMLString: webviewHTMLString baseURL: tempImageURL]; doesn't block until the rendering is complete. Changed code as follows:

{   
    .
    .
    .

    // Load the frame, print it in the delegate when the load is complete   
    [printView setFrameLoadDelegate: self];
    [[printView mainFrame] loadHTMLString: webviewHTMLString baseURL: tempImageURL];
}

//  WebView has completed loading, so it can be printed now.
- (void) webView: (WebView *) printView didFinishLoadForFrame: (WebFrame *)frame
{
    NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];

    NSPrintOperation *printOp = [NSPrintOperation printOperationWithView: [[[printView mainFrame] frameView] documentView] printInfo: printInfo];
    [printOp setShowsPrintPanel: YES];
    [printOp runOperation];
    [printView release];
}

这篇关于WebView(OSX)无法在打印面板预览中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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