2023-09-18 10:25:43 +08:00
|
|
|
|
package com.engine.salary.util.pdf;
|
|
|
|
|
|
|
|
|
|
|
|
import com.engine.workflow.biz.requestForm.HtmlToPdfInterceptor;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class HtmlToPdf {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* html转pdf
|
|
|
|
|
|
*
|
2023-09-18 18:45:59 +08:00
|
|
|
|
* @param toPdfTool 工具路径
|
2023-09-18 10:25:43 +08:00
|
|
|
|
* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径
|
|
|
|
|
|
* @param destPath pdf保存路径
|
|
|
|
|
|
* @return 转换成功返回true
|
|
|
|
|
|
*/
|
2023-09-18 18:45:59 +08:00
|
|
|
|
public static boolean convert(String toPdfTool, String srcPath, String destPath) {
|
2023-09-18 10:25:43 +08:00
|
|
|
|
File file = new File(destPath);
|
|
|
|
|
|
File parent = file.getParentFile();
|
|
|
|
|
|
//如果pdf保存路径不存在,则创建路径
|
|
|
|
|
|
if (!parent.exists()) {
|
|
|
|
|
|
parent.mkdirs();
|
|
|
|
|
|
}
|
|
|
|
|
|
StringBuilder cmd = new StringBuilder();
|
|
|
|
|
|
cmd.append(toPdfTool);
|
|
|
|
|
|
cmd.append(" ");
|
|
|
|
|
|
cmd.append(" --header-line");//页眉下面的线
|
|
|
|
|
|
cmd.append(" --margin-top 3cm ");//设置页面上边距 (default 10mm)
|
|
|
|
|
|
// cmd.append(" --header-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\head.html"));// (添加一个HTML页眉,后面是网址)
|
|
|
|
|
|
cmd.append(" --header-spacing 5 --orientation Portrait ");// (设置页眉和内容的距离,默认0)
|
|
|
|
|
|
//cmd.append(" --footer-center (设置在中心位置的页脚内容)");//设置在中心位置的页脚内容
|
|
|
|
|
|
//cmd.append(" --footer-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\foter.html"));// (添加一个HTML页脚,后面是网址)
|
|
|
|
|
|
cmd.append(" --footer-line");//* 显示一条线在页脚内容上)
|
|
|
|
|
|
cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
|
|
|
|
|
|
//--orientation Portrait --footer-center [page]/[topage] --footer-line
|
|
|
|
|
|
cmd.append(srcPath);
|
|
|
|
|
|
cmd.append(" ");
|
|
|
|
|
|
cmd.append(destPath);
|
|
|
|
|
|
boolean result = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
Process proc = Runtime.getRuntime().exec(cmd.toString());
|
|
|
|
|
|
HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
|
|
|
|
|
|
HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
|
|
|
|
|
|
error.start();
|
|
|
|
|
|
output.start();
|
|
|
|
|
|
proc.waitFor();
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
result = false;
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|