一、前言
本文基于springboot2.4.0
通过wkhtmltoimage
和wkhtmltopdf
工具实现HTML转图片和PDF
此工具个人测试不会丢失css样式,在转换方面还算比较nice
二、下载wkhtmltopdf
自己下载安装即可…
https://wkhtmltopdf.org/downloads.html
centos7安装如下:
# 下载
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos7.x86_64.rpm
# 安装
yum install xorg-x11-fonts-75dpi.noarch
rpm -Uvh wkhtmltox-0.12.6-1.centos7.x86_64.rpm
# 查看安装位置
whereis wkhtmltoimage
# 测试
wkhtmltoimage --crop-w 150 --crop-h 150 --quality 100 https://zhengqing.blog.csdn.net zhengqingya.png
解决中文字体不显示问题
# ---------------------- ↓↓↓↓↓↓ 解决中文字体不显示问题 ↓↓↓↓↓↓ ----------------------
# 查看是否有中文字体
fc-list :lang=zh
# 下载中文字体`simsun.ttc`,放到`/usr/share/fonts`目录下
cd /usr/share/fonts
# 注:在此字体目录下直接下载,可能存在识别不出字体情况,需要手动将`simsun.ttc`字体放到`/usr/share/fonts`目录下
wget https://gitee.com/zhengqingya/java-developer-document/blob/master/%E5%B7%A5%E5%85%B7/wkhtmltopdf/simsun.ttc
三、工具类
温馨小提示:windows下的工具路径修改为自己的路径即可~
import cn.hutool.core.io.FileUtil;
import com.zhengqing.demo.config.Constants;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* <p>
* html 转 图片或pdf 工具类
* </p>
*
* @author zhengqingya
* @description https://wkhtmltopdf.org
* html转pdf: wkhtmltopdf https://zhengqing.blog.csdn.net zhengqingya.pdf
* html转图片: wkhtmltoimage https://zhengqing.blog.csdn.net zhengqingya.png
* 帮助 wkhtmltopdf -h 或 wkhtmltoimage -h
* @date 2021/8/11 9:54 下午
*/
@Slf4j
public class WkHtmlUtil {
/**
* 工具根目录
*/
private static final String TOOL_WIN_ROOT_DIRECTORY = "D:/zhengqingya/soft/soft-dev/wkhtmltopdf/bin/";
public static void main(String[] args) throws Exception {
String sourceFilePath = "https://zhengqing.blog.csdn.net";
String targetPngFilePath = Constants.DEFAULT_FOLDER_TMP_GENERATE + "/zhengqingya.png";
String targetPdfFilePath = Constants.DEFAULT_FOLDER_TMP_GENERATE + "/zhengqingya.pdf";
// 设置宽高
String cmdByImage = "--crop-w 150 --crop-h 150 --quality 100";
byte[] imageBytes = html2ImageBytes(cmdByImage, sourceFilePath, targetPngFilePath);
byte[] pdfBytes = html2PdfBytes("", sourceFilePath, targetPdfFilePath);
}
/**
* html转图片
*
* @param cmd 工具操作指令
* @param sourceFilePath html源资源
* @param targetFilePath 生成目标资源
* @return 图片字节码
* @author zhengqingya
* @date 2021/8/12 11:09
*/
public static byte[] html2ImageBytes(String cmd, String sourceFilePath, String targetFilePath) {
return baseTool("wkhtmltoimage", cmd, sourceFilePath, targetFilePath);
}
/**
* html转pdf
*
* @param cmd 工具操作指令
* @param sourceFilePath html源资源
* @param targetFilePath 生成目标资源
* @return pdf字节码
* @author zhengqingya
* @date 2021/8/12 11:09
*/
public static byte[] html2PdfBytes(String cmd, String sourceFilePath, String targetFilePath) {
return baseTool("wkhtmltopdf", cmd, sourceFilePath, targetFilePath);
}
/**
* 工具基础操作
*
* @param tool 工具
* @param cmd 工具操作指令
* @param sourceFilePath html源资源
* @param targetFilePath 生成目标资源
* @return 字节码
* @author zhengqingya
* @date 2021/8/12 11:08
*/
@SneakyThrows({Exception.class})
private static byte[] baseTool(String tool, String cmd, String sourceFilePath, String targetFilePath) {
// 先创建父目录
FileUtil.mkParentDirs(targetFilePath);
String command = String.format("%s %s %s %s", getToolRootPath() + tool, cmd, sourceFilePath, targetFilePath);
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
throw new Exception("工具丢失,请联系系统管理员!");
}
// 等待当前命令执行完,再往下执行
process.waitFor();
log.info("=============== FINISH: [{}] ===============", command);
return FileUtil.readBytes(targetFilePath);
}
/**
* 根据不同系统获取工具
*
* @return 工具位置
* @author zhengqingya
* @date 2021/8/12 11:07
*/
private static String getToolRootPath() {
String system = System.getProperty("os.name");
if (system.contains("Windows")) {
return TOOL_WIN_ROOT_DIRECTORY;
} else if (system.contains("Linux") || system.contains("Mac OS X")) {
return "";
}
return "";
}
}
四、本文案例demo源码
https://gitee.com/zhengqingya/file-convert-util
今日分享语句:
每一个清晨,记得提醒自己:
没有奇迹,只有你努力的轨迹;
没有运气,只有你坚持的勇气。
每一分收获,都来自你坚持不懈的努力;
每一分汗水,都是你成功的积累。
只要相信自己,总会遇到惊喜!
未来我们的生活,一切全靠自己!