【问题标题】:How to print pdf in Vaadin FlowHow to print pdf in Vaadin Flow
【发布时间】:2022-12-02 08:23:42
【问题描述】:

I'm trying to print a PDF with Vaadin Flow, unfortunately I can't really get it to work or rather I don't know how to go about it. I have found the following entry, but here is always the whole browser page printed I really want to print only a PDF. Could someone please help me.

How to Print in Vaadin Flow?

【问题讨论】:

    标签: vaadin-flow


    【解决方案1】:

    When I want to print a PDF file with Vaadin Flow, I use Print.Js.

    First, I create a Javascript source file somewhere in the frontend directory, such as frontend/imports/import-print-js.js, which is used to import Print.Js in the next step. Put the following line in that file:

    import printJS from 'print-js';
    

    Then I add the Print.Js NPM package to the project and import its JS module by annotating the Java view/component class I'll be using to call Print.Js from as follows:

    @NpmPackage(value = "print-js", version = "1.6.0")
    @JsModule("./imports/import-print-js.js")
    

    Then to print the PDF, use the Java code

    UI.getCurrent().getPage().executeJs("printJS(%s)".formatted(PARAMETER_STRING));
    

    where PARAMETER_STRING contains the parameters you need/want to print your PDF file. For instance, if you have a document docs/mydocument.pdf on your local server and you want to show Print.Js's user feedback modal dialog, your PARAMETER_STRING would be:

    {printable:'docs/mydocument.pdf', type:'pdf', showModal:true}
    

    For details on the parameters available, see the Print.Js documentation.

    【讨论】:

      猜你喜欢
      • 2023-02-25
      • 1970-01-01
      • 2022-12-26
      • 1970-01-01
      • 2022-12-28
      • 2022-12-02
      • 2022-12-26
      • 2022-12-26
      • 2022-11-20
      相关资源
      最近更新 更多