简体   繁体   English

从javascript,JSP或Java运行Phantomjs

[英]Running Phantomjs from javascript, JSP or Java

Hi i am new to phantomjs, 嗨,我是phantomjs的新手,

I have generated HTML to PDF by using command. 我已使用命令生成HTML到PDF。 But i want to generate PDF by clicking a button on the page. 但我想通过单击页面上的按钮来生成PDF。 and call phantomjs by some way to generate my given URL to pdf. 并通过某种方式调用phantomjs来生成我的给定URL到pdf。

You can also suggest some api that generate generate PDF as HTML with charts and images and can easily integrated with JSP and Servlet. 您还可以建议使用图表和图像生成生成PDF格式的API,并且可以轻松地与JSP和Servlet集成。

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. 在您的情况下,您需要首先知道要执行的命令的完整路径,phantomjs。 If you downloaded the zip, this is the directory to which you unzipped the file in, where you see the phantomjs.exe executable. 如果您下载了zip,则这是您将文件解压缩到的目录,您可以在其中看到phantomjs.exe可执行文件。 If you downloaded it through package manager, to find out the full path run from a terminal: 如果您是通过包管理器下载的,那么要查找从终端运行的完整路径:

which phantomjs

Which will display something like 这将显示类似的东西

/usr/bin/phantomjs

Once you have that, you'll have to use the Runtime class, which, among other things, lets you run commands directly on the OS using exec. 一旦你有了这个,你将不得不使用Runtime类,除其他外,它允许你使用exec直接在操作系统上运行命令。 What you run, will then be handled as a Process which you can use to read the output of the command from. 您运行的内容将作为一个进程处理 ,您可以使用该进程从中读取命令的输出。

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外部进程,如果您不能很好地管理错误,可能会对程序的其余部分产生问题。

From phantomjs release 1.8 is available Ghost Driver, an implementation of WebDriver Wire Protocol. 从phantomjs发布1.8版本可以使用Ghost Driver,它是WebDriver Wire Protocol的一个实现。

It allow to start phantomjs as remote server enabling http communication with it. 它允许启动phantomjs作为远程服务器启用与它的http通信。

$ phantomjs --webdriver=PORT

This makes easy integration with whatever programming language 这使得可以轻松地与任何编程语言集成

For further details take a look here 有关详细信息,请查看此处

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

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