html2image使用

实例代码
File file = new File(scjkConfig.getApkPath()+"temp/");
  if (!file.exists()) {
      file.mkdirs();
  }
  String pic_path = scjkConfig.getApkPath()+"temp/"+waterPlants.getId()+".png";
  try{
      String filePath = waterModel.getPath().replace("/files/", "");
      String url = scjkConfig.getUploadPath()+filePath;
      File templateFile = new File(url);
      //读取文件
       String content = FileUtils.readFileToString(templateFile, "utf-8");
      //创建一个解析器
      Document doc = Jsoup.parse(content);
      Element body = doc.body();
      body.attr("style","width:1366px;height:768px;");
      Elements es_template = doc.getElementsByClass("template");
      Element template = es_template.get(0);
      //java.net.URLEncoder.encode(basePath+waterModel.getImagePath(), "GBK");
      template.attr("style","background-image:url('"+basePath+waterModel.getImagePath()+"');background-repeat:no-repeat");
      Elements es_technology = doc.getElementsByClass("technology");
      Iterator it = es_technology.iterator();
      if(StringUtils.isNotNull(waterModel.getTemplateIds())){
      while(it.hasNext()) {
          Element element = (Element) it.next();
           element.attr("style","border:none;");
          String divId =  element.attr("id");
          String[] tid_did = waterModel.getTemplateIds().split(",");
          for(String tdId:tid_did){
              String tid =  tdId.substring(0,tdId.indexOf("-"));
              String did =  tdId.substring(tdId.indexOf("-")+1,tdId.length());
              if(divId.equals(did)){
                  for(TemplateModel tm:tms){
                      if(tid.equals(tm.getId()+"")){
                          element.html("<div style=\"background:#2E6B87;color:white;min-height:25px;line-height:25px;padding:2px;text-align:center;border-radius:5px;font-size:13px;\">"+tm.getName()+"<span style=\"color:#3FD5F5;\">"+tm.getValuez()+"</span>"+tm.getUnit()+"</div>");
                      }

                  }

              }
          }
      }

      }
       html2Image = Html2Image.fromHtml(doc.html());
      System.out.println(doc.html());
       //html2Image = Html2Image.fromHtml(document.toString());
  }catch (Exception e){
      System.out.print(e);
  }

if(html2Image!=null){
    html2Image.getImageRenderer().saveImage(pic_path);
}
  waterModel.setImagePath(pic_path.replaceAll(scjkConfig.getApkPath(),"/appFiles/"));
  result.put("waterModel",waterModel);

2、注意事项

找不到类错误就是jar包不符合,可到github官网代码里找到pom文件引入相应的子jar版本
jsoup爬虫得到html的内容

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值