gpt4 book ai didi

macos - WebView (OSX) 未在打印面板预览中呈现

转载 作者:行者123 更新时间:2023-12-03 16:11:02 26 4
gpt4 key购买 nike

我的应用程序创建并打印 WebView。输出页面正在正确组装和打印,但我没有在打印面板中预览页面。

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 在后台打印,我会得到一个空白页:

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

有什么想法吗?

最佳答案

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

{   
.
.
.

// 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];
}

关于macos - WebView (OSX) 未在打印面板预览中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18216560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com