安装wkhtmltopdf插件

1.wkhtmltopdf官网(https://wkhtmltopdf.org/downloads.html)下载安装包;

alt wkhtmltopdf官网截图

安装到linux

  1. 上传到linux服务器;
  2. 解压:
    Ubuntu:dpkg -i wkhtmltox_0.12.5-1.trusty_amd64.deb;
    Centos:rpm -ivh wkhtmltox-0.12.5-1.centos7.x86_64.rpm;
  3. 安装依赖

    yum install -y fontconfig libX11 libXext libXrender libjpeg libpng xorg-x11-fonts-75dpi xorg-x11-fonts-Type1 openssl
  4. 安装windows字体参考地址:http://blog.cnsyear.cn/article/z05qax.html

安装到windows

1.下载win32或win64版本解压即可

alt

java调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 String wkhtmltopdfPath="F:\wkhtmltopdf\bin\wkhtmltopdf.exe";
String url="https://www.baidu.com";
Runtime rt = Runtime.getRuntime();
String command = "";
//判断当前操作系统
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){ // for windows
command = wkhtmltopdfPath+ " \"" + url + "\" "+ \pdfPath\xxx.pdf;
}else{ // for linux
command = " wkhtmltopdf '" +url+ "' "+/pdfPath/xxx.pdf;
}
Process p = rt.exec(command);
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sbf = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sbf.append(tmp);
}
String resultStr = sbf.toString(); //执行command命令返回的结果

注:在windows或linux控制台窗口下执行所对应的”command”与上面代码效果一致

参考链接:https://www.jianshu.com/p/328e203a51a6