springboot项目 java将html转为图片

public static void main(final String[] args) throws Exception {
//html页面中需要灵活变动的值
    String headurl = "";//图片的路径不能是带有参数的 不然解析不了
    String proUrl = "";
    String qrCodeUrl = "";
    String proName="犀牛皮特质皮鞋";
    String proPrice="1000.00";
    String userName="测试";
    String html = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\"/>\n" +
            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>\n" +
            "    <title>分享</title>\n" +
            "    <style>\n" +
            "        body {\n" +
            "            font-size: 0;\n" +
            "            padding: 0;\n" +
            "            margin: 0;\n" +
            "        }\n" +
            "\n" +
            "        div {\n" +
            "            box-sizing: border-box;\n" +
            "        }\n" +
            "\n" +
            "        .main {\n" +
            "            width: 654px;\n" +
            "            margin: 0 auto;\n" +
            "            padding: 28px;\n" +
            "            text-align: center;\n" +
            "            background: green;\n" +
            "        }\n" +
            "\n" +
            "        .top{\n" +
            "            display: inline-block;\n" +
            "            max-width: 100%;\n" +
            "            margin: 0 auto 20px;\n" +
            "            position: relative;\n" +
            "            padding-left: 56px;\n" +
            "            font-size: 28px;\n" +
            "            line-height: 48px;\n" +
            "            color: #666;\n" +
            "        }\n" +
            "\n" +
            "        .oneOver {\n" +
            "            overflow: hidden;\n" +
            "            text-overflow: ellipsis;\n" +
            "            white-space: nowrap;\n" +
            "        }\n" +
            "\n" +
            "        .head{\n" +
            "            height: 48px;\n" +
            "            width: 48px;\n" +
            "            border-radius: 50%;\n" +
            "            overflow: hidden;\n" +
            "            border: solid 1px #E6E6E6;\n" +
            "            position: absolute;\n" +
            "            left: 0;\n" +
            "            top: 0;\n" +
            "            overflow: hidden;\n" +
            "        }\n" +
            "\n" +
            "        .head img {\n" +
            "            width: 100%;\n" +
            "            height: 100%;\n" +
            "        }\n" +
            "\n" +
            "        .pro_pic{\n" +
            "            width: 400px;\n" +
            "            height: 400px;\n" +
            "            margin: 0 auto 24px;\n" +
            "        }\n" +
            "\n" +
            "        .pro_pic_{\n" +
            "            width: 100%;\n" +
            "            height: 100%;\n" +
            "        }\n" +
            "\n" +
            "        .money{\n" +
            "            font-size: 40px;\n" +
            "            font-weight: bold;\n" +
            "            padding-bottom: 16px;\n" +
            "        }\n" +
            "\n" +
            "        .name{\n" +
            "            font-size: 28px;\n" +
            "            line-height: 40px;\n" +
            "            font-weight: bold;\n" +
            "            padding-bottom: 34px;\n" +
            "        }\n" +
            "\n" +
            "        .code{\n" +
            "            height: 175px;\n" +
            "            width: 175px;\n" +
            "            margin: 0 auto 20px;\n" +
            "            border: solid 1px #E6E6E6;\n" +
            "            border-radius: 50%;\n" +
            "            overflow: hidden;\n" +
            "        }\n" +
            "\n" +
            "        .code_pic{\n" +
            "            height: 175px;\n" +
            "            width: 175px;\n" +
            "        }\n" +
            "\n" +
            "        .desc{\n" +
            "            font-size: 24px;\n" +
            "            line-height: 34px;\n" +
            "            color: #999;\n" +
            "        }\n" +
            "    </style>\n" +
            "</head>\n" +
            "<body>\n" +
            "    <div class=\"main\" id=\"main\">\n" +
            "        <div class=\"top oneOver\">\n" +
            "            <div class=\"head\">\n" +
            "                <img class=\"persion_pic\" src=\""+headurl+"\"/>\n" +
            "            </div>\n" +
            "            "+userName+",为您安利好物\n" +
            "        </div>\n" +
            "        <div class=\"pro_pic\">\n" +
            "            <img class=\"pro_pic_\" src=\""+proUrl+"\"/>\n" +
            "        </div>\n" +
            "        <div class=\"money\">¥"+proPrice+"</div>\n" +
            "        <div class=\"name\">"+proName+"</div>\n" +
            "        <div class=\"code\">\n" +
            "            <img class=\"code_pic\" src=\""+qrCodeUrl+"\"/>\n" +
            "        </div>\n" +
            "        <div class=\"desc\">识别小程序码,查看商品详情</div>\n" +
            "    </div>\n" +
            "</body>\n" +
            "</html>";
//string转为inputstream流
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(html.getBytes());
    File targetFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+"share1.html");
//inputstream流转为file
    FileUtils.copyInputStreamToFile(byteArrayInputStream, targetFile);
//通过openhtmltopdf工具生成图片
    final Java2DRenderer renderer = new Java2DRenderer(targetFile, 740, 1000);
    final BufferedImage img = renderer.getImage();
    final FSImageWriter imageWriter = new FSImageWriter();
    imageWriter.setWriteCompressionQuality(0.9f);
    imageWriter.write(img, "output.png");//输出路径
    System.out.println("Done with rendering");

}

pom.xml文件

<dependency>
    <groupId>com.openhtmltopdf</groupId>
    <artifactId>openhtmltopdf-core</artifactId>
    <version>0.0.1-RC9</version>
</dependency>

生成的图片

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值