phantomjs自动截图生成图片
https://www.jianshu.com/p/41d5e08af0a7
https://www.cnblogs.com/bangejingting/p/6907628.html
有时候我们需要定时的生成一些报表图片,或者通过钉钉等发送给对应用户或群组;
第一步,本地生成报表,可以用baiduEchart等
https://www.echartsjs.com/zh/tutorial.html#%E4%B8%AA%E6%80%A7%E5%8C%96%E5%9B%BE%E8%A1%A8%E7%9A%84%E6%A0%B7%E5%BC%8F
第二部,自动渲染图片,进行截图
springMvc.xml添加: <bean id="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration"init-method="init"><property name="configFileResource" value="classpath:beetl.properties" /> </bean><bean id="beelViewResolver" class="org.beetl.ext.spring.BeetlSpringViewResolver"><property name="suffix" value=".html" /><property name="contentType" value="text/html;charset=UTF-8" /><property name="order" value="0"/><property name="config" ref="beetlConfig"/><property name="cache" value="true"/><property name="allowSessionOverride" value="true" /><property name="allowRequestOverride" value="true" /> </bean>maven依赖: <!-- 在线生成图片导出 --> <dependency><groupId>com.ibeetl</groupId><artifactId>beetl</artifactId><version>2.5.0</version> </dependency> <dependency><groupId>com.dingtalk.chatbot</groupId><artifactId>dingtalk-chatbot-sdk</artifactId><version>0.9.0-SNAPSHOT</version> </dependency>
try {//渲染模版,生成HTML代码Template template = groupTemplate.getTemplate(templateFile);template.binding("data", data);template.binding("title", title);template.binding("subTitle", subTitle);String html = template.render();return html; } catch (Exception e) {log.warn("convertDataToGraph error", e); }
//写入到文件 private String convertHtmlToImage(String html, EnvType env) throws IOException {//写入到文件String[] commands = null;writeToFile(html, API_RANK_RELEASE_HTML);commands = new String[]{"bash", "-c", phantomPath + " " + reportPath + CAPTURE_RELEASE_JS};//生成图片int status = 1;Process process = Runtime.getRuntime().exec(commands, null, new File(reportPath));status = process.waitFor();//执行名称出错,直接抛出异常if (status != 0) {log.error("failed to generate the image status code:{}", status);throw new RuntimeException("failed to generate the image status code: " + status);}return imageTempPath; }
private void writeToFile(String html, String tempHtmlName) throws IOException {BufferedWriter writer = null;String fileName = reportPath + tempHtmlName;try {File oldFile = new File(fileName);if (oldFile.exists()) {oldFile.delete();}writer = new BufferedWriter(new FileWriter(fileName));writer.write(html);} finally {if (writer != null) {writer.close();}} }
beetl.properties #\u914D\u7F6E\u5F15\u64CE\u5B9E\u73B0\u7C7B ENGINE=org.beetl.core.engine.FastRuntimeEngine #\u6307\u5B9A\u4E86\u5360\u4F4D\u7B26\u53F7\uFF0C\u9ED8\u8BA4\u662F${ }.\u4E5F\u53EF\u4EE5\u6307\u5B9A\u4E3A\u5176\u4ED6\u5360\u4F4D\u7B26 DELIMITER_PLACEHOLDER_START=${ DELIMITER_PLACEHOLDER_END=} #\u6307\u5B9A\u4E86\u8BED\u53E5\u7684\u5B9A\u754C\u7B26\u53F7\uFF0C\u9ED8\u8BA4\u662F<% %>,\u4E5F\u53EF\u4EE5\u6307\u5B9A\u4E3A\u5176\u4ED6\u5B9A\u754C\u7B26\u53F7 #DELIMITER_STATEMENT_START=@ #DELIMITER_STATEMENT_END=null #\u6307\u5B9AIO\u8F93\u51FA\u6A21\u5F0F\uFF0C\u9ED8\u8BA4\u662FFALSE,\u5373\u901A\u5E38\u7684\u5B57\u7B26\u8F93\u51FA\uFF0C\u518D\u8003\u8651\u9AD8\u6027\u80FD\u60C5\u51B5\u4E0B\uFF0C\u53EF\u4EE5\u8BBE\u7F6E\u6210true DIRECT_BYTE_OUTPUT = true #\u6307\u5B9A\u4E86\u652F\u6301HTML\u6807\u7B7E\uFF0C\u4E14\u7B26\u53F7\u4E3A#\uFF08\u53EA\u80FD\u7528\u4E00\u4E2A\u7B26\u53F7\u6807\u793A\uFF09\uFF0C\u9ED8\u8BA4\u914D\u7F6E\u4E0B\uFF0C\u6A21\u677F\u5F15\u64CE\u8BC6\u522B<#tag ></#tag> #\u8FD9\u6837\u7684\u7C7B\u4F3Chtml\u6807\u7B7E\uFF0C\u5E76\u80FD\u8C03\u7528\u76F8\u5E94\u7684\u6807\u7B7E\u51FD\u6570\u3002 HTML_TAG_SUPPORT = true HTML_TAG_FLAG = # #\u6307\u5B9A\u5141\u8BB8\u672C\u5730Class\u76F4\u63A5\u8C03\u7528 NATIVE_CALL = TRUE #\u6307\u5B9A\u6A21\u677F\u5B57\u7B26\u96C6\u662FUTF-8 TEMPLATE_CHARSET = UTF-8 #\u6307\u5B9A\u5F02\u5E38\u7684\u89E3\u6790\u7C7B\uFF0C\u9ED8\u8BA4\u662FConsoleErrorHandler\uFF0C\u4ED6\u5C06\u5728render\u53D1\u751F\u5F02\u5E38\u7684\u65F6\u5019\u5728\u540E\u53F0\u6253\u5370\u51FA\u9519\u8BEF\u4FE1\u606F(System.out)\u3002 ERROR_HANDLER = org.beetl.core.ConsoleErrorHandler #\u6307\u5B9A\u4E86\u672C\u5730Class\u8C03\u7528\u7684\u5B89\u5168\u7B56\u7565 NATIVE_SECUARTY_MANAGER= org.beetl.core.DefaultNativeSecurityManager #\u6307\u5B9A\u4E86\u9ED8\u8BA4\u4F7F\u7528\u7684\u6A21\u677F\u8D44\u6E90\u52A0\u8F7D\u5668 RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader #\u914D\u7F6E\u4E86\u662F\u5426\u8FDB\u884C\u4E25\u683CMVC\uFF0C\u901A\u5E38\u60C5\u51B5\u4E0B\uFF0C\u6B64\u5904\u8BBE\u7F6E\u4E3Afalse. MVC_STRICT = FALSE #\u8D44\u6E90\u914D\u7F6E\uFF0Cresource\u540E\u7684\u5C5E\u6027\u53EA\u9650\u4E8E\u7279\u5B9AResourceLoader #classpath \u6839\u8DEF\u5F84(\u5176\u5B9E\u5C31\u662F\u524D\u7F00\u7684\u914D\u7F6E) RESOURCE.root= /report/ #\u662F\u5426\u68C0\u6D4B\u6587\u4EF6\u53D8\u5316 RESOURCE.autoCheck=true #\u81EA\u5B9A\u4E49\u811A\u672C\u65B9\u6CD5\u6587\u4EF6\u7684Root\u76EE\u5F55\u548C\u540E\u7F00 #RESOURCE.functionRoot = functions #RESOURCE.functionSuffix = fn #\u81EA\u5B9A\u4E49\u6807\u7B7E\u6587\u4EF6Root\u76EE\u5F55\u548C\u540E\u7F00 #RESOURCE.tagRoot = htmltag #RESOURCE.tagSuffix = tag##### \u6269\u5C55 ############## ## \u5185\u7F6E\u7684\u65B9\u6CD5 #FN.date = org.beetl.ext.fn.DateFunction #FN.nvl = org.beetl.ext.fn.NVLFunction #FN.debug = org.beetl.ext.fn.DebugFunction #FN.exist = org.beetl.ext.fn.CheckExistFunction #FN.printf = org.beetl.ext.fn.Printf #FN.decode = org.beetl.ext.fn.DecodeFunction #FN.assert = org.beetl.ext.fn.AssertFunction #FN.print = org.beetl.ext.fn.Print #FN.println = org.beetl.ext.fn.Println #FN.trunc = org.beetl.ext.fn.TruncFunction #FN.trim = org.beetl.ext.fn.TruncFunction2 #FN.empty = org.beetl.ext.fn.EmptyFunction #FN.qmark = org.beetl.ext.fn.QuestionMark #FN.isEmpty = org.beetl.ext.fn.EmptyExpressionFunction #FN.parseInt = org.beetl.ext.fn.ParseInt #FN.parseDouble= org.beetl.ext.fn.ParseDouble #FN.range = org.beetl.ext.fn.Range ##\u5185\u7F6E\u7684\u529F\u80FD\u5305 #FNP.strutil = org.beetl.ext.fn.StringUtil #FNP.array = org.beetl.ext.fn.ArrayUtil##\u5185\u7F6E\u7684\u683C\u5F0F\u5316\u51FD\u6570 #FT.dateFormat = org.beetl.ext.format.DateFormat #FT.numberFormat = org.beetl.ext.format.NumberFormat ##\u5185\u7F6E\u7684\u9ED8\u8BA4\u683C\u5F0F\u5316\u51FD\u6570 #FTC.java.util.Date = org.beetl.ext.format.DateFormat #FTC.java.sql.Date = org.beetl.ext.format.DateFormat #FTC.java.sql.Time = org.beetl.ext.format.DateFormat #FTC.java.sql.Timestamp = org.beetl.ext.format.DateFormat #FTC.java.lang.Short = org.beetl.ext.format.NumberFormat #FTC.java.lang.Long = org.beetl.ext.format.NumberFormat #FTC.java.lang.Integer = org.beetl.ext.format.NumberFormat #FTC.java.lang.Float = org.beetl.ext.format.NumberFormat #FTC.java.lang.Double = org.beetl.ext.format.NumberFormat #FTC.java.math.BigInteger = org.beetl.ext.format.NumberFormat #FTC.java.math.BigDecimal = org.beetl.ext.format.NumberFormat #FTC.java.util.concurrent.atomic.AtomicLong = org.beetl.ext.format.NumberFormat #FTC.java.util.concurrent.atomic.AtomicInteger = org.beetl.ext.format.NumberFormat## \u6807\u7B7E\u7C7B #TAG.include= org.beetl.ext.tag.IncludeTag #TAG.includeFileTemplate= org.beetl.ext.tag.IncludeTag #TAG.layout= org.beetl.ext.tag.LayoutTag #TAG.delete= org.beetl.ext.tag.DeleteTag #TAG.htmltag= org.beetl.ext.tag.HTMLTagSupportWrapper #WEBAPP_EXT = com.ximalaya.southgate.admin.beetl.GlobalExt
captureRelease.jsvar page = require('webpage').create(); page.viewportSize = {width: 1920,height: 1080,orientation: 'landscape' }; page.open('apiRankRelease.html', function() {window.setTimeout(function () {page.render('apiRankRelease.png');page.close();phantom.exit();}, 3000);});
reportPieRelease.html<!DOCTYPE html> <html style="height: 100%"> <head><meta charset="utf-8"><style>@font-face {font-family: "PingF";src: url("font/PingFang-SC-Bold.ttf"),url("font/PingFang-SC-Light.ttf"),url("font/PingFang-SC-Regular.ttf");}body {font-family: "PingF";}</style> </head> <body style="height: 100%; margin: 0"> <div id="container" style=" width: 1920px;height: 1080px"></div> <div id="text">push </div> <!--<script type="text/javascript" src="../lib/echarts/4/echarts.min.js"></script>--> <script src="http://echarts.baidu.com/dist/echarts.min.js"></script> <script type="text/javascript">setTimeout(function () {var text = document.getElementById("text");text.style.display="none";var dom = document.getElementById("container");var myChart = echarts.init(dom);var data = genData(20);option = {title : {text: '${title}',subtext: '${subTitle}',x:'center',textStyle:{fontFamily: "PingF",fontSize:25},subtextStyle:{fontFamily: "PingF",fontSize:23}},tooltip : {trigger: 'item',formatter: "{a} <br/>{b} : {c} ({d}%)",textStyle: {fontFamily: "PingF",fontSize: 20}},legend: {type: 'scroll',orient: 'vertical',right: 10,top: 80,bottom: 20,data: data.legendData,selected: data.selected,textStyle: {fontFamily: "PingF",fontSize: 23}},series : [{name: '姓名',type: 'pie',radius : '35%',center: ['30%', '45%'],data: data.seriesData,itemStyle: {emphasis: {shadowBlur: 10,shadowOffsetX: 0,shadowColor: 'rgba(0, 0, 0, 0.5)'}},label:{ //饼图图形上的文本标签normal:{show:true,textStyle : {fontFamily: "PingF",fontSize : 18 //文字的字体大小}}}}]};function genData(count) {var legendData = [];var seriesData = [];var selected = {};var i = 0;<%for(apiItem in data){%>name = '${apiItem.product}';legendData.push(name);seriesData.push({name: name,value: ${apiItem.amount}});i++;selected[name] = i < 10;<%}%>return {legendData: legendData,seriesData: seriesData,selected: selected};};if (option && typeof option === "object") {myChart.setOption(option, true);}}, 1000); </script> </body> </html>
phantomjs自动截图生成图片相关推荐
- 苹果小圆点怎么关闭_苹果手机连按2下屏幕,就能自动截图,不知道的来学一学...
苹果手机连按2下屏幕,就能自动截图,不知道的来学一学 很多苹果用户都羡慕安卓手机,可以花式截屏,不仅支持局部截屏还可以长截屏. 其实苹果手机的截屏方式也可以很炫酷,今天就来教大家一招,连按2下手机屏幕 ...
- python延时函数_Python实现屏幕自动截图
教程目录 需要实现的功能:自动屏幕截图 具体需求: 1. 支持设置截图频率和截图文件存储路径 2. 在存储截图时判断与前一张截图的相似度,只有屏幕发生了显著的变化才存储截图 所需技术(搜索关键词): ...
- phantomjs linux 中文不显示,linux 安装phantomJs 用于截图,处理中文乱码,和样式问题...
1,下载安装phantomJs 下载后解压文件,看情况配置环境变量 tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 2 相关踩坑 2.1 问题演示 中文乱 ...
- TestNG监听器实现失败自动截图、重跑、自定义html结果文件功能
本文转载自孔庆云kevin<TestNG监听器实现失败自动截图.重跑.自定义html结果文件功能> 使用Testng框架搭建自动测试框架,经常会需要增加失败自动截图,以及失败重跑功能,下面 ...
- python 批量打开网页并截图_Python自动截图html页面
需求:实现自动截图html页面: 准备: 1.安装selenium 1)pip install selenium 2) 通过pycharm解释器图形化安装 File->settings-> ...
- 淘宝商品价格变化监控并自动截图网页为证
相信大家都会有监控某网页的需求,比如招标网信息更新监控,淘宝网商品信息更新监控,网上商城商品信息更新监控等等.供大家获得一手信息或者价格变化情况:同时,方便经销商,厂家.供货商等监控商品价格异动,防止 ...
- 91发票查验助手——批量查验并自动截图保存
批量查验发票并截图保存的软件--91发票查验助手 91发票查验助手--批量查验并自动截图保存 91发票查验助手--批量查验并自动截图保存 我之前是手动查的,查的我眼花,后来发现太蠢了--百度了好久,最 ...
- python自动截图保存(网课利器)
最近线上报告太多,老师要求我对会议ppt进行截图.手动截图很耗时,保存命名也比较麻烦.有时候刚打开截图,那一页就跳过去了--所以想到用python编写一个自动截图保存的程序,这样就能安心听报告啦. 库 ...
- macbook android截屏,配合Alfred的mac端自动截图脚本(ios android适用)
1.需求背景 在进行客户端测试过程中常常需要在手机上截图并上传到电脑上,过程比较繁琐,配合alfred和mac系统的command格式文件特性编写了自动截图脚本. 2.展示 command双击执行 配 ...
- Excel重设格式并自动截图保存为图片
使用Pandas模块生成的数据,在导出保存为Excel 时,是一种默认的格式,常常遇到列宽无法完整显示单元格中的文本.文字大小.行距不是预期的,无边框.无行背景色等问题,必须手动重设格式. 而且,现在 ...
最新文章
- RT-Thread优化智能车设计
- 用Java获取文件的MD5校验和
- 17._5正则表达式的替换
- linux离线安装ftp_安装Kali Linux之后要做的前10件事
- 数据中心机房设计及各专业技术平衡
- python函数调用语句_Python函数定义和函数调用
- httpHandlers
- MATLAB 中RMSE和MAPE的计算方法
- python顺序执行 toggle_pythonkivymd:如何使toggle_nav_drawer()函数工作?
- Audacity Mac版教程,使用Audacity编辑音频波形图的方法步骤
- Unity动画系统详解5:BlendTree混合树是什么?
- 机器学习:使用matlab实现逻辑回归解决数字识别(多元分类)问题
- ST电机库 MCSDK-FUL_5.4.4 下载
- php文件格式,php是什么文件格式
- 云和恩墨大讲堂 - Oracle RAC系列课程强势来袭
- windows 微信手机端退出登录,pc电脑端不退出的奇淫技巧
- linux dx游戏,10大免费 Linux 游戏
- OAS、Swagger和Springfox
- 解决“DNS_PROBE_FINISHED_NXDOMAIN”,访问网站打不开问题
- 1055 mysql_mysql8.0.1 报错1055