java实现pdf打印工具类,Java PDF工具类(二)| 使用 wkhtmltox 实现 HTML转PDF(文字/图片/页眉页脚)...

import lombok.extern.slf4j.Slf4j;

import java.io.*;

import java.nio.charset.StandardCharsets;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

/**

* 生成PDF工具类

*/

@Slf4j

public class HtmlToPdfUtils {

/**

* wkhtmltopdf在 Windows、Linux系统中的安装路径

*/

private static final String WINDOWS_HTML_TO_PDF_TOOL = "E:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

private static final String LINUX_HTML_TO_PDF_TOOL = "/opt/wkhtmltox/bin/wkhtmltopdf";

/**

* 页眉图片html:Windows、Linux系统中的路径

*/

private static final String WINDOWS_HEAD_HTML = "D:\\head.html";

private static final String LINUX_HEAD_HTML = "/opt/head.html";

/**

* 页脚图片html:Windows、Linux系统中的路径

*/

private static final String WINDOWS_FOOT_HTML = "D:\\foot.html";

private static final String LINUX_FOOT_HTML = "/opt/foot.html";

/**

* 临时文件存放目录:Windows、Linux系统中的路径

*/

public static final String WINDOWS_FILE_URL = "D:\\pdf\\temporary\\file";

public static final String LINUX_FILE_URL = "/opt/temporary/file";

static {

String fileUrl = isWindowsSystem() ? WINDOWS_FILE_URL : LINUX_FILE_URL;

File f = new File(fileUrl);

if(!f.exists()){

f.mkdirs();

}

}

/**

* html字符串转pdf,会生成垃圾文件,需要定时清理

*

* @param data 替换好的html字符串

* @param destFileName 保存pdf的名称

* @return 返回pdf成功生成的路径

*/

public static String convertStringToHtml(String data, String destFileName) {

return convert(stringToHtml(data), destFileName);

}

/**

* 判断当前系统是否是Windows系统

* @return true:Windows系统,false:Linux系统

*/

public static boolean isWindowsSystem(){

String property = System.getProperty("os.name").toLowerCase();

return property.contains("windows");

}

/**

* html转pdf(加页眉页脚)

*

* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径

* @param destFileName 保存pdf的名称

* @return 返回pdf成功生成的路径

*/

public static String convert(String srcPath, String destFileName) {

destFileName = (isWindowsSystem() ? WINDOWS_FILE_URL + "\\" : LINUX_FILE_URL + "/") + destFileName;

File file = new File(destFileName);

// File parent = file.getParentFile();

//如果pdf保存路径不存在,则创建路径

if (!file.exists()) {

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

log.error("html转pdf,创建文件失败:{}", destFileName);

}

}

StringBuilder cmd = new StringBuilder();

String toPdfTool = isWindowsSystem() ? WINDOWS_HTML_TO_PDF_TOOL : LINUX_HTML_TO_PDF_TOOL;

// 这里可以拼接页眉页脚等参数

cmd.append(toPdfTool);

cmd.append(" ");

// wkhtmltopdf默认不允许访问本地文件,需加入以下参数

cmd.append(" --enable-local-file-access");

// cmd.append(" --page-size A4");

cmd.append(" ");

cmd.append(" --disable-smart-shrinking");

// 页眉图片

cmd.append(" --header-html " + (isWindowsSystem() ? WINDOWS_HEAD_HTML : LINUX_HEAD_HTML));

cmd.append(" --header-spacing 5");

// 页脚图片

cmd.append(" --footer-html " + (isWindowsSystem() ? WINDOWS_FOOT_HTML : LINUX_FOOT_HTML));

cmd.append(" " + srcPath);

cmd.append(" ");

cmd.append(destFileName);

try {

log.info("html转pdf命令:{}", cmd.toString());

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();

log.info("html转pdf成功:html路径:{} pdf保存路径:{}", srcPath, destFileName);

} catch (Exception e) {

destFileName = "";

e.printStackTrace();

log.error("html转pdf失败:html路径:{} pdf保存路径:{}", srcPath, destFileName);

}

return destFileName;

}

/**

* html字符串转pdf,会生成临时html文件,需要定时清理

*

* @param data 替换好的html字符串

* @return 返回生成的临时文件名称

*/

public static String stringToHtml(String data) {

String srcPath = "";

OutputStreamWriter osw = null;

try {

// 生成随机名字

String code = String.format("%04d",(int) ((Math.random()*9+1) * 1000));

String nowDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

// 根据系统,创建字符输出流对象,负责向文件内写入

if (!isWindowsSystem()) {

// 非Windows 系统

srcPath = LINUX_FILE_URL + "/" +String.format("%s%s", code , nowDate) + ".html";

} else {

srcPath = WINDOWS_FILE_URL + "\\" + String.format("%s%s", code , nowDate) + ".html";

}

osw = new OutputStreamWriter(new FileOutputStream(srcPath), StandardCharsets.UTF_8);

log.info("html转pdf,生成临时文件:{}", srcPath);

// 将str里面的内容读取到fw所指定的文件中

osw.write(data);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

log.error("html转pdf,生成临时文件失败:{}", e);

}finally{

if(osw!=null){

try {

osw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

log.error("html转pdf,生成临时文件关流失败:{}", e);

}

}

}

return srcPath;

}

public static void main(String[] args) {

HtmlToPdfUtils.convert("D:\\test.html", "D:\\wkhtmltopdf.pdf");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值