使用wkhtmltopdf也有一段时间了,这里把使用过程中碰到的问题整理一下
1.wkhtmltopdf安装
从官网下载对应对应环境的版本
下载地址:https://wkhtmltopdf.org/downloads.html
傻瓜式解压安装,这里不细说
测试是否安装成功,进入到程序执行目录(\wkhtmltopdf\bin)
wkhtmltopdf "https://www.baidu.com" baidu.pdf
查看当前目录文件是否生成baidu.pdf
(注意:如果是Linux环境需要设置权限chmod 777 *
,如果未将wkhtmltopdf设置到PATH用./wkhtmltopdf "https://www.baidu.com" baidu.pdf
测试)
2、java调用代码
测试代码
/**
* 测试
*/
public class Test {
private File exportPDF(String fileName, String url, String outFileName) {
String exePath;
String param;
String filePath;
String os = System.getProperty("os.name");//获取当前操作系统名称
if (os.toLowerCase().startsWith("win")) {//如果是windows系统
exePath = "D:/wkhtmltopdf/bin/wkhtmltopdf.exe";
param = "--page-size A4 --page-height 297 --page-width 210 --javascript-delay 1000 --no-stop-slow-scripts --load-error-handling ignore";
filePath = System.getProperty("java.io.tmpdir") + File.separator;
} else {
exePath = "/app/tomcat/wkhtmltopdf/bin/wkhtmltopdf";
param = "--page-size A4 --page-height 297 --page-width 210 --javascript-delay 1000 --no-stop-slow-scripts --load-error-handling ignore";
filePath = "/app/tomcat/sendfile/";
}
return null;
}
public static void main(String[] args) {
StringBuilder cmd = new StringBuilder();
cmd.append("D:/wkhtmltopdf/bin/wkhtmltopdf.exe");//wkhtmltopdf程序所在位置
cmd.append(" ");
cmd.append("--page-size A2");// 参数
cmd.append(" ");
cmd.append("https://123.sogou.com https://123.sogou.com https://123.sogou.com");//可多个
cmd.append(" ");
cmd.append("D:/pdf_test.pdf");//保存路径
WKLin wu = new WKLin();
wu.winExec(cmd.toString());
}
}
封装代码
/**
* 在Linux和window上执行应用程序(cmd)
* @author Administrator
*
*/
public class WKLin{
//linux
public void exec(String cmd){
InputStream ins = null;
String[] cmds = new String[] {"/bin/sh","-c",cmd};
try {
Process process = Runtime.getRuntime().exec(cmds);
ins = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitValue = process.waitFor();
System.out.println( exitValue);
process.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}finally{
}
}
//window
public void winExec(String cmd) {
InputStream ins = null;
String[] cmds = new String[] { "cmd.exe", "/C", "start "+cmd }; // 创建命令
try {
Process process = Runtime.getRuntime().exec(cmds);
ins = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int exitValue = process.waitFor();
process.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、问题整理
1、Linux服务器上需把指令中的 (
进行转义 \(
,否则提示异常fail load XXXXX
2、生成的文件名不能出现*?等特殊符号,否则提示异常:Error: Unable to write to destination
3、Linux上调用指令:./wkhtmltopdf-i386 "https://www.baidu.com" baidu.pdf
注意url要加双引号,否则会把路径直接当做指令来执行
4、在web项目中集成wkhtmltopdf,但是进行转换后项目都会自动跳转到登陆页面
原因:项目配置了安全控制,访问路径不正确或session过期时自动跳转到登陆界面,使用WKLin调用wkhtmltopdf时类同于使用另外一个浏览器进行访问,使得访问的session为空
解决:在调用wkhtmltopdf设置cookie,wkhtmltopdf提供了对应的参数位:
--cookie <name> <value>
name表示cookie名称,value表示cookie值 ,可以设置多个
ps:
StringBuilder cmd = new StringBuilder();
cmd.append(toPdfTool);
cmd.append(" ");
cmd.append(" --cookie "+cookie.getName()+" "+cookie.getValue()+" ");//设置cookie name及value 属性
cmd.append(" --header-line");//页眉下面的线
cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
//cmd.append(" --margin-top 30mm ");//设置页面上边距 (default 10mm)
cmd.append(" --header-spacing 10 ");// (设置页眉和内容的距离,默认0)
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
注意:cookie的位数是有限的,配置过多会直接报错Not enough arguments parsed to --cookie