从 javascript、JSP 或 Java 运行 Phantomjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16889950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:22:40  来源:igfitidea点击:

Running Phantomjs from javascript, JSP or Java

javajavascriptjspphantomjs

提问by user2439207

Hi i am new to phantomjs,

嗨,我是 phantomjs 的新手,

I have generated HTML to PDF by using command. But i want to generate PDF by clicking a button on the page. and call phantomjs by some way to generate my given URL to pdf.

我已使用命令将 HTML 生成为 PDF。但我想通过单击页面上的按钮来生成 PDF。并通过某种方式调用 phantomjs 来生成我给定的 pdf 的 URL。

You can also suggest some api that generate generate PDF as HTML with charts and images and can easily integrated with JSP and Servlet.

您还可以推荐一些 api,它们可以生成带有图表和图像的 HTML 格式的 PDF,并且可以轻松地与 JSP 和 Servlet 集成。

回答by chamakits

I'm assuming that what you want to do is to run the phantomjs executable from within Java code.

我假设您想要做的是从 Java 代码中运行 phantomjs 可执行文件。

You'll need to first know the full path of the command you want to execute, in your case, phantomjs. If you downloaded the zip, this is the directory to which you unzipped the file in, where you see the phantomjs.exe executable. If you downloaded it through package manager, to find out the full path run from a terminal:

您首先需要知道要执行的命令的完整路径,在您的情况下为 phantomjs。如果您下载了 zip,这是您将文件解压缩到的目录,您可以在其中看到 phantomjs.exe 可执行文件。如果您是通过包管理器下载的,从终端找出完整路径:

which phantomjs

Which will display something like

这将显示类似

/usr/bin/phantomjs

Once you have that, you'll have to use the Runtimeclass, which, among other things, lets you run commands directly on the OS using exec. What you run, will then be handled as a Processwhich you can use to read the output of the command from.

一旦你有了它,你就必须使用Runtime类,除其他外,它允许你使用 exec 直接在操作系统上运行命令。您运行的内容将被处理为一个进程,您可以使用它来读取命令的输出。

A quick example without any of the Exception handling that you SHOULD be doing.

一个简单的例子,没有你应该做的任何异常处理。

    Process process = Runtime.getRuntime().exec("/usr/bin/phantomjs myscript.js");
    int exitStatus = process.waitFor();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader (process.getInputStream()));

    String currentLine=null;
    StringBuilder stringBuilder = new StringBuilder(exitStatus==0?"SUCCESS:":"ERROR:");
    currentLine= bufferedReader.readLine();
    while(currentLine !=null)
    {
        stringBuilder.append(currentLine);
        currentLine = bufferedReader.readLine();
    }
    System.out.println(stringBuilder.toString());

Make sure to do proper error handling, as you are creating process external to the JVM, which the JVM doesn't exactly control, and could create issues to the rest of your program if you don't manage errors well.

确保进行正确的错误处理,因为您正在 JVM 外部创建进程,JVM 无法完全控制这些进程,如果您不能很好地管理错误,可能会给程序的其余部分带来问题。

回答by bsorrentino

From phantomjs release 1.8 is available Ghost Driver, an implementation of WebDriver Wire Protocol.

从 phantomjs 1.8 版开始,可以使用 Ghost 驱动程序,这是 WebDriver Wire 协议的实现。

It allow to start phantomjs as remote server enabling http communication with it.

它允许将 phantomjs 启动为远程服务器,启用与它的 http 通信。

$ phantomjs --webdriver=PORT

$ phantomjs --webdriver=PORT

This makes easy integration with whatever programming language

这可以与任何编程语言轻松集成

For further details take a look here

有关更多详细信息,请查看此处