生成pdf phantomjs (2)

该内容可以参考https://echarts.apache.org/examples/zh/index.html 官网,寻找自己需要的图表样式

{ title : { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip : { trigger: 'axis' }, legend: { data:['最高气温','最低气温'] }, toolbox: { show : false, feature : { mark : {show: true}, dataView : {show: true, readOnly: false}, magicType : {show: true, type: ['line', 'bar']}, restore : {show: true}, saveAsImage : {show: true} } }, calculable : true, xAxis : [ { type : 'category', boundaryGap : false, data : ['周一','周二','周三','周四','周五','周六','周日'] } ], yAxis : [ { type : 'value', axisLabel : { formatter: '{value} °C' } } ], series : [ { name:'最高气温', type:'line', data:[11, 11, 15, 13, 12, 13, 10], markPoint : { data : [ {type : 'max', name: '最大值'}, {type : 'min', name: '最小值'} ] }, markLine : { data : [ {type : 'average', name: '平均值'} ] } }, { name:'最低气温', type:'line', data:[1, -2, 2, 5, 3, 2, 0], markPoint : { data : [ {name : '周最低', value : -2, xAxis: 1, yAxis: -1.5} ] }, markLine : { data : [ {type : 'average', name : '平均值'} ] } } ] }

htmlToPdf.js

var page = require('webpage').create(); var system = require('system'); ////读取命令行参数,也就是js文件路径。 if (system.args.length === 1) { console.log('Usage: loadspeed.js <some URL>'); //这行代码很重要。凡是结束必须调用。否则phantomjs不会停止 phantom.exit(); } page.settings.loadImages = true; //加载图片 page.settings.resourceTimeout = 10000;//超过10秒放弃加载 //缩放比例 page.zoomFactor=1; // 设置浏览器 page.settings.userAgent='Mozilla/5.0 AppleWebKit/537.36 (KHTML,like Gecko) Chrome/33.0.1750.117 Safari/537.36'; //截图设置, //page.viewportSize = { // width: 1000, // height: 3000 //}; // 设置尺寸 page.paperSize={format: 'A4',orientation:'portrait',margin:'0.8cm',border:'1cm'}; // page.paperSize = { width:'1500px',height:'2000px',orientation: 'portrait',border: '1cm' }; var address = system.args[1]; var outpathstr = system.args[2]; page.open(address, function(status) { function checkReadyState() {//等待加载完成将页面生成pdf setTimeout(function () { var readyState = page.evaluate(function () { return document.readyState; }); if ("complete" === readyState) { // var timestamp = Date.parse(new Date()); // var pdfname = 'HT_'+timestamp + Math.floor(Math.random()*1000000); // var outpathstr = "/Users/zachary/Downloads/phantomjs2/bin/"+pdfname+".pdf"; setTimeout(function () { page.render(outpathstr); //console.log就是传输回去的内容。 console.log("生成成功"); console.log("$"+outpathstr+"$"); phantom.exit(); },200); } else { checkReadyState(); } },200); } checkReadyState(); });

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyspy.html