第一次使用wkhtmltox

时间:2022-05-27 23:17:52

==========================================================

下载地址:http://wkhtmltopdf.org/downloads.html

学习地址:http://www.tuicool.com/articles/uQbu2a
                  http://www.blogjava.net/supercrsky/articles/176525.html

-----------------------------------------------------------------------------------------------------

下载安装好后可以如图把wkhtmltox配置到环境变量中去,方便使用

第一次使用wkhtmltox


测试代码:

import java.io.*;

public class HtmlToImage{

public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wkhtmltoimage",
"www.baidu.com", "D:\\test.png");
Process process;
try{
process = pb.start();
BufferedReader errStreamReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
System.out.println("read errStreamReader");
String line = null;
line = errStreamReader.readLine();
while(line != null){
System.out.println(line);
line = errStreamReader.readLine();
}
process.destroy();
System.out.println("destroyed process");
} catch (IOException e) {
e.printStackTrace();
}
}
}


测试调用命令行:以后便于使用wkhtmltox

import java.io.*;

public class CommandTest{

public static void main(String[] args) {
//open notepad
// try {
// Process proc=Runtime.getRuntime().exec("notepad");
// } catch (IOException e) {
// e.printStackTrace();
// }

//open baidu by IE
// try {
// String exeFullPathName="C:/Program Files/Internet Explorer/IEXPLORE.EXE";
// String message="www.baidu.com";
// String[] cmd={exeFullPathName,message};

// Process proc=Runtime.getRuntime().exec(cmd);

// } catch (IOException e) {
// e.printStackTrace();
// }

try {
String[] cmd = {"cmd", "/c", "C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe"};
Runtime.getRuntime().exec(cmd);
//why 这里不知道为什么不可以直接用,希望看到的会的指点下
//Runtime.getRuntime().exec("cmd /c start C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe");

} catch (IOException e) {
e.printStackTrace();
}

}
}