wkhtmltoimage生成图片并打包下载

思路:
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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值