思路:
1.wkhtmltoimage生成图片
2.ZipOutputStream将图片打包
3.返回前端包路径
public String packImage(List<String> ids) throws Exception {
//方案一
// response.setCharacterEncoding(StandardCharsets.UTF_8.displayName());
// response.setContentType("multipart/form-data");
// response.setHeader("Content-Disposition", "attachment;filename=" + UUIDUtil.getUuid() + ".zip");
// packImage(response, ids);
String downUrl = "";
Map<String,String> idMap = new HashMap<>();
for (String id : ids) {
String destPath = downUrl + id + ".png";
String srcPath = "https://www.baidu.com";
HtmlToImage.convert(srcPath, destPath,toImageTool);
idMap.put(id,destPath);
}
//只有一个证书文件 直接返回原文件
if(idMap.size() == 1){
for (String id : idMap.keySet()) {
return downUrl + id +".png";
}
}
String zipName = UUIDUtil.getUuid() + ".zip";
// 设置压缩包的名字
String zipDownUrl = downUrl + zipName;
// 合并压缩文件
reBuildCertificateFile(idMap, zipDownUrl, ".png");
return zipDownUrl;
}
生成图片
public class HtmlToImage {
/**
* html转image
*
* @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径
* @param destPath 保存路径
* @return 转换成功返回true
*/
public static boolean convert(String srcPath, String destPath,String toImageTool) {
File file = new File(destPath);
File parent = file.getParentFile();
//如果保存路径不存在,则创建路径
if (!parent.exists()) {
parent.mkdirs();
}
StringBuilder cmd = new StringBuilder();
cmd.append(toImageTool);
cmd.append(" ");
cmd.append(srcPath);
cmd.append(" ");
cmd.append(destPath);
boolean result = true;
try {
Process proc = Runtime.getRuntime().exec(cmd.toString());
HtmlToImageInterceptor error = new HtmlToImageInterceptor(proc.getErrorStream());
HtmlToImageInterceptor output = new HtmlToImageInterceptor(proc.getInputStream());
error.start();
output.start();
proc.waitFor();
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}
/**
* 当java调用wkhtmltoimage时,用于获取wkhtmltoimage返回的内容
*/
private static class HtmlToImageInterceptor extends Thread {
private InputStream is;
public HtmlToImageInterceptor(InputStream is) {
this.is = is;
}
@Override
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line); //输出内容
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打包图片
private void reBuildCertificateFile(Map<String,String> idMap, String zipDownUrl, String imageType) throws Exception {
File file = new File(zipDownUrl);
File parent = file.getParentFile();
//如果保存路径不存在,则创建路径
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream fos = new FileOutputStream(zipDownUrl);
ZipOutputStream zipos = new ZipOutputStream(fos);
zipos.setMethod(ZipOutputStream.DEFLATED);
DataOutputStream os = null;
// 循环将文件写入压缩流
for (String certCode : idMap.keySet()) {
String filePath = idMap.get(certCode);
String fileName = certCode;
// 添加ZipEntry,并ZipEntry中写入文件流
zipos.putNextEntry(new ZipEntry(fileName + imageType));
os = new DataOutputStream(zipos);
InputStream is = new FileInputStream(filePath);
byte[] b = new byte[4096];
int length = 0;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
}
os.flush();
os.close();
zipos.close();
}