简体   繁体   English

如何使用PDF.js在WebView(JavaFX)面板中显示PDF文件?

[英]How can I display a PDF file in a WebView (JavaFX) panel using PDF.js?

I got this ( https://stackoverflow.com/a/42040344/3789572 ) solution, which code is below, but it isn't working. 我得到了这个( https://stackoverflow.com/a/42040344/3789572 )解决方案,下面的代码,但是不起作用。 When I press the button, nothing is shown. 当我按下按钮时,什么也没显示。 You can change the file paths and try it for yourself. 您可以更改文件路径,然后自己尝试。

Can you help me? 你能帮助我吗?

Controller code: 控制器代码:

package sample.principal;

import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

import java.io.File;  
import java.net.URL;
import java.util.Base64;
import java.util.ResourceBundle;

import org.apache.commons.io.FileUtils;

public class WebController implements Initializable {

@FXML
private WebView web;

@FXML
private Button btn;

public void initialize(URL location, ResourceBundle resources) {
    WebEngine engine = web.getEngine();
    String url = getClass().getResource("..\\resources\\web\\viewer.html").toExternalForm();

    // connect CSS styles to customize pdf.js appearance
    engine.setUserStyleSheetLocation(getClass().getResource("..\\resources\\web\\viewer.css").toExternalForm());

    engine.setJavaScriptEnabled(true);
    engine.load(url);

    engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
                // to debug JS code by showing console.log() calls in IDE console
                JSObject window = (JSObject) engine.executeScript("window");
                window.setMember("java", new JSLogListener());
                engine.executeScript("console.log = function(message){ java.log(message); };");

                // this pdf file will be opened on application startup
                if (newValue == Worker.State.SUCCEEDED) {
                    try {
                        // readFileToByteArray() comes from commons-io library
                        byte[] data = FileUtils.readFileToByteArray(new File("C:\\Users\\Felipe\\Documents\\" +
                                "Programação\\Java\\" +
                                "IdeaProjects\\PDFviewerStackOverFlow\\src\\sample\\principal\\teste.pdf"));
                        String base64 = Base64.getEncoder().encodeToString(data);
                        // call JS function from Java code
                        engine.executeScript("openFileFromBase64('" + base64 + "')");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

    // this file will be opened on button click
    btn.setOnAction(actionEvent -> {
        try {
            byte[] data = FileUtils.readFileToByteArray(new File("teste.pdf"));
            String base64 = Base64.getEncoder().encodeToString(data);
            engine.executeScript("openFileFromBase64('" + base64 + "')");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
}

The Main code: 主要代码:

package sample.principal;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

public static void main(String[] args) {
    Application.launch();
}

public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("PDF test app");
    primaryStage.setScene(new Scene(root, 1280, 576));
    primaryStage.show();
}
}

and the other one: 另一个:

package sample.principal;

public class JSLogListener {
public void log(String text) {
    System.out.println(text);
}
}

I would be very grateful for your help. 谢谢您的帮助。

Thanks. 谢谢。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage stage) {
    stage.setTitle("HTML");
    stage.setWidth(500);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    VBox root = new VBox();     

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);
    webEngine.loadContent("<b>asdf</b>");

    root.getChildren().addAll(scrollPane);
    scene.setRoot(root);

    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
     launch(args);
  }
  }

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

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