【问题标题】:Vaadin 14 Open new component in new Browser tabVaadin 14 在新的浏览器选项卡中打开新组件
【发布时间】:2020-03-11 01:55:11
【问题描述】:

有没有办法可以打开嵌入到组件中的 pdf 并发送到新的浏览器选项卡?

想法是将资源设置为此组件并在新选项卡中打开。稍后我将需要在更多按钮中导航不同的文档。

public class EmbeddedPdfDocument extends Component implements HasSize {

    public EmbeddedPdfDocument(StreamResource resource) {
        this();
        getElement().setAttribute("data", resource);
    }

    public EmbeddedPdfDocument(String url) {
        this();
        getElement().setAttribute("data", url);
    }

    protected EmbeddedPdfDocument() {
        getElement().setAttribute("type", "application/pdf");
        setSizeFull();
    }
}

【问题讨论】:

  • 您的问题越来越不清楚。我怀疑你真的在问什么应该是两个单独的问题帖子。
  • 我想我会尝试你在另一个问题上提到的这种方法。我将构建一个 UI 并通过参数加载它们。 stackoverflow.com/questions/51490046/…

标签: java pdf vaadin vaadin14


【解决方案1】:

Vaadin.com 论坛中的This thread 讨论您的问题。

Anchor::setTarget"_blank"

使用Anchor 小部件作为您的链接。见demo page

➥ 关键是将“目标”设置为字符串_blank

String url = "…" ;
Anchor anchor = new Anchor( url , "Open a PDF document" ) ;
anchor.setTarget( "_blank" ) ;  // Specify `_blank` to open in a new browser tab/window.

这是 Vaadin 14.1.19 中基于 Plain Java Servlet 品种的 starter project 的完整示例应用程序。

运行此示例应用程序。单击该链接可查看另一个 Web 浏览器选项卡打开并显示 PDF 文档。

package work.basil.example;

import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
// @PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
@CssImport ( "./styles/shared-styles.css" )
@CssImport ( value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field" )
public class MainView extends VerticalLayout
{

    public MainView ( )
    {
        // Widgets
        H1 heading = new H1( "Download PDF in browser tab" );

        String url = "https://www.fda.gov/media/76797/download";
        Anchor anchor = new Anchor( url , "Open a PDF document" );
        anchor.setTarget( "_blank" );  // Specify `_blank` to open in a new browser tab/window.

        // Arrange
        this.add( heading , anchor );
    }
}

【讨论】:

  • 您好,抱歉,我添加了一张图片以使其更清晰。我想知道我们是否可以设计一个具有嵌入 pdf 的工具栏的组件并发送到一个新选项卡以供用户导航和阅读不同的 pdf?
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 2014-03-19
  • 2018-08-29
  • 1970-01-01
  • 2021-03-25
相关资源
最近更新 更多