如何在Vaadin Flow中打印?

如何在Vaadin Flow中打印?,vaadin,vaadin-flow,Vaadin,Vaadin Flow,我想从一个按钮打印出用Vaadin 14创建的主页的内容 不幸的是,Vaadin 8是一种不再适用的解决方案: Button print = new Button("Print This Page"); print.addClickListener(new Button.ClickListener() { public void buttonClick(ClickEvent event) { // Print the current page JavaSc

我想从一个按钮打印出用Vaadin 14创建的主页的内容

不幸的是,Vaadin 8是一种不再适用的解决方案:

Button print = new Button("Print This Page");
print.addClickListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        // Print the current page
        JavaScript.getCurrent().execute("print();");
    }
});

您知道如何在Vaadin14中执行此操作吗?

在Vaadin10及更高版本中,您可以通过调用

在那里,您应该能够调用相同的打印函数。请参阅:

因此,在您的情况下,要打印当前页面:

UI.getCurrent().getPage().executeJs( "print();" ) ;
Vaadin 14.0.12中使用lambda语法的完整示例:

package com.example;

import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
public class MainView extends VerticalLayout
{
    public MainView ( )
    {
        this.add( new H1( "Print example" ) );

        Button printButton = new Button( "Print…" );
        printButton.addClickListener( ( ClickEvent < Button > clickEvent ) ->
        {
            UI.getCurrent().getPage().executeJs( "print();" );
        } );
        this.add(printButton);
    }
}
package.com.example;
导入com.vaadin.flow.component.ClickEvent;
导入com.vaadin.flow.component.UI;
导入com.vaadin.flow.component.button.button;
导入com.vaadin.flow.component.html.H1;
导入com.vaadin.flow.component.orderedlayout.VerticalLayout;
导入com.vaadin.flow.router.Route;
/**
*主视图包含一个按钮和一个单击侦听器。
*/
@路线(“”)
公共类主视图扩展了垂直布局
{
公共主视图()
{
添加(新H1(“打印示例”);
按钮printButton=新按钮(“打印…”);
printButton.addClickListener((ClickEvent
对于使用Vaadin 8而非的用户,请参见类似问题:
package com.example;

import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
public class MainView extends VerticalLayout
{
    public MainView ( )
    {
        this.add( new H1( "Print example" ) );

        Button printButton = new Button( "Print…" );
        printButton.addClickListener( ( ClickEvent < Button > clickEvent ) ->
        {
            UI.getCurrent().getPage().executeJs( "print();" );
        } );
        this.add(printButton);
    }
}