搭建highcharts server,将图表发送至邮箱

发布于 2018-03-13 作者 shen100 554次 浏览 版块 分享

安装highcharts-export-server

npm install highcharts-export-server -g

如果没有安装node.js,请先安装node.js

在安装的过程中,报错

Writing location.js file
Error checking path, continuing { Error: EACCES: permission denied, open '/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/location.js'
    at Object.fs.openSync (fs.js:652:18)
    at Object.fs.writeFileSync (fs.js:1299:33)
    at writeLocationFile (/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/util.js:84:6)
    at Promise._successFn (/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/install.js:375:11)
    at nextTickCallback (/usr/lib/node_modules/highcharts-export-server/node_modules/kew/kew.js:47:28)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  errno: -13,
  code: 'EACCES',
  syscall: 'open',
  path: '/usr/lib/node_modules/highcharts-export-server/node_modules/phantomjs-prebuilt/lib/location.js' }

添加--unsafe-perm选项,然后再次运行

npm install highcharts-export-server -g --unsafe-perm

启动highcharts server

通过下面的命令来启动端口为 3000 的 http 服务

highcharts-export-server --enableServer 1 --port 3000

安装nodemailer

npm install nodemailer

安装axios

npm install axios

示例

const nodemailer = require('nodemailer');
const axios = require('axios');

const params = {
    host: '',
    port: 465,
    sercure: true,
    auth: {
        user: '', 
        pass: ''
    }
}

const mailOptions = {
    from: '',
    to: [''],
    subject: '',
    html: ''
}

let chartData = {
    b64: true, // 返回base64字符串,而不是二进制
    infile: {
        title: {
            text: 'Steep Chart'
        }, 
        xAxis: {
            categories: [
                'Jan', 
                'Feb', 
                'Mar'
            ]
        }, 
        series: [
            {
                data: [
                    29.9, 
                    71.5, 
                    106.4
                ]
            }
        ]
    }
};

let url = 'http://127.0.0.1:3000';  // 如果highcharts-export-server  安装在另一台机器上,记得修改此处的ip

axios.post(url, chartData)
    .then(function (response) {
        console.log(response);
        if (response && response.status === 200) {
            let imgBase64 = 'data:image/png;base64,' + response.data;
            mailOptions.html = [
                `<div>`,
                `<img src=${imgBase64} />`,
                `</div>`,
            ].join('');

            const transporter = nodemailer.createTransport(params)
            transporter.sendMail(mailOptions, (error, info) => {
                if (error) {
                    return console.log(error);
                }
                console.log('Message %s sent: %s', info.messageId, info.response);
                process.exit(0);
            });
        }
    })
    .catch(function (error) {
        console.log(error);
    });
收藏
2回复
hujunyuan80581楼•2018-03-17

bucuo

liushen2楼•2018-03-17

up