package com.json.manager.controller.util;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
import org.anyline.net.HttpResult;
import org.anyline.net.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
public class HtmlToPdf {
//wkhtmltopdf在系统中的路径
//private static final String toPdfTool = "F://wkhtml//wkhtmltopdf//bin//wkhtmltopdf.exe";
/**
* html转pdf
* toPdfTool:D\:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf
*
* @param toPdfTool 应用软件安装路径
* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径htmlPath + "/js/mg/typical/caseinfo/caseM?id=" + ID
* @param destPath pdf保存路径src/main/resources/templates/
* @return 转换成功返回true
*/
public static boolean convert(String toPdfTool, String srcPath, String destPath) {
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(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
// cmd.append(" --viewport-size 1280x1024 ");
cmd.append(" --page-size A4 ");
//cmd.append(" --margin-top 30mm ");//设置页面上边距 (default 10mm)
cmd.append(" --header-spacing 5 ");// (设置页眉和内容的距离,默认0)
// cmd.append(" --disable-smart-shrinking ");
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
boolean result = true;
Process proc = null;
HtmlToPdfInterceptor error = null;
HtmlToPdfInterceptor output = null;
try {
proc = Runtime.getRuntime().exec(cmd.toString());
error = new HtmlToPdfInterceptor(proc.getErrorStream());
output = new HtmlToPdfInterceptor(proc.getInputStream());
java使用wkhtmltopdf将html页面生成pdf并且加水印
最新推荐文章于 2024-07-09 17:42:18 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)