简体   繁体   English

在 Python (Gtk3.0) 中打印 WebView 的内容

[英]Print contents of WebView in Python (Gtk3.0)

I have designed a very simple Python3 app that uses a WebView to render Markdown files.我设计了一个非常简单的 Python3 应用程序,它使用 WebView 来呈现 Markdown 文件。 Now I'd like to add the possibility to print the rendered file (with the current stylesheet that's being applied).现在我想添加打印渲染文件的可能性(使用当前正在应用的样式表)。 I'm desperately trying to find some information about how to do it.我拼命地试图找到一些关于如何做到这一点的信息。 Is there any tutorial or howto out there that could explain how to do it?是否有任何教程或howto可以解释如何做到这一点?

I've found a few examples explaining how to print PDF files from disk, but I'd like to print things directly from the WebView.我找到了一些示例来解释如何从磁盘打印 PDF 文件,但我想直接从 WebView 打印内容。 Apparently there is even a Method for that but I'm failing miserably on figuring out on how to use it...显然,甚至还有一种方法,但我在弄清楚如何使用它时失败了......

See here这里

Best Regards Merlin最好的问候梅林

OK, I finally figured it out.好的,我终于想通了。 It appears that only the Webkit2 library is modern enough to handle printing from a Webview.似乎只有 Webkit2 库足够现代,可以处理来自 Webview 的打印。 I was using WebKit1 and that was the reason it wouldn't work.我使用的是 WebKit1,这就是它不起作用的原因。 So I had to change my dependencies and after that it was actually quite easy...所以我不得不改变我的依赖关系,之后它实际上很容易......

Lets say that you have a Webview whose name is myWebView , then your function to print it's content would look like:假设您有一个名为 myWebView 的myWebView ,然后您的 function 打印它的内容如下所示:

def onPrintButtonClicked( self, widget ):
    #Set the operation up
    printOperation = WebKit2.PrintOperation.new( self.myWebView )
    #Run the dialog
    printOperation.run_dialog()

After the dialog, you could actually check if the user has chosen to print or to cancel by checking for WebKit2.PrintOperationResponse.CANCEL or WebKit2.PrintOperationResponse.PRINT return values.在对话框之后,您实际上可以通过检查WebKit2.PrintOperationResponse.CANCELWebKit2.PrintOperationResponse.PRINT返回值来检查用户是否选择了打印或取消。

Reagards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM