首页 > 解决方案 > AWS Lambda 上的 Highcharts 导出服务器

问题描述

是否有人成功创建了在 AWS Lambda 上运行的 highcharts 导出服务器?如果有,他们可能愿意分享一个示例部署包。

总体目标是能够将图像类型和图表选项作为有效负载传递给 lambda 并让它返回图像。

到目前为止,我已经完成了以下工作:

# Created a folder and moved into it
cd ../highchart_export_server

# Im building Phantom on OSX do I need to set these to get Phantom to build
# for linux
export PHANTOMJS_PLATFORM="linux"
export PHANTOMJS_ARCH="x64"

# Created a new package and install highcharts per docs
npm init
npm install highcharts-export-server

# Create an index handler -> See the content of that file below

# zip the package up as a deployment and upload it to lambda
zip -r function.zip .

index.js

//Include the exporter module
const exporter = require('highcharts-export-server');

exports.handler = async (event) => {

    var type = event.body.type;
    console.log('Type: ' + type);

    //Export settings
    var exportSettings = {
        type: event.format,
        options: event.options;

    //Set up a pool of PhantomJS workers
    exporter.initPool();

    //Perform an export
    /*
        Export settings corresponds to the available CLI arguments described
        above.
    */
    exporter.export(exportSettings, function (err, res) {
        if (err) {
            console.log(err.stack);
        }

        //The export result is now in res.
        //If the output is not PDF or SVG, it will be base64 encoded (res.data).
        //If the output is a PDF or SVG, it will contain a filename (res.filename).

        //Kill the pool when we're done with it, and exit the application
        exporter.killPool();
        process.exit(1);
    });


    const response = {
        statusCode: 200,
        body: event,
    };
    return response;
};

当我测试该功能时,我收到此错误:

START RequestId: 97e615c4-5e42-457e-8c8a-02e7001957f5 Version: $LATEST
2019-07-18T15:20:57.128Z    97e615c4-5e42-457e-8c8a-02e7001957f5    ERROR   Uncaught Exception  {"errorType":"Error","errorMessage":"write EPIPE","code":"EPIPE","stack":["Error: write EPIPE","    at WriteWrap.afterWrite [as oncomplete] (net.js:779:14)"],"errno":"EPIPE","syscall":"write"}
2019-07-18T15:20:57.207Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    uncaughtException: { Error: write EPIPE
    at WriteWrap.afterWrite [as oncomplete] (net.js:779:14)
  errno: 'EPIPE',
  code: 'EPIPE',
  syscall: 'write',
  [Symbol(aws.lambda.Timestamp)]: 2019-07-18T15:20:57.128Z,
  [Symbol(aws.lambda.RequestId)]: '97e615c4-5e42-457e-8c8a-02e7001957f5' }
2019-07-18T15:20:57.247Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 1 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.247Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 2 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.247Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 3 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.247Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 4 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.248Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 5 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.248Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 6 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.248Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 7 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.248Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    Thu Jul 18 2019 15:20:57 GMT+0000 (Coordinated Universal Time) [error] phantom worker 8 error - /var/task/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory
2019-07-18T15:20:57.268Z    97e615c4-5e42-457e-8c8a-02e7001957f5    INFO    undefined
END RequestId: 97e615c4-5e42-457e-8c8a-02e7001957f5
REPORT RequestId: 97e615c4-5e42-457e-8c8a-02e7001957f5  Duration: 620.04 ms Billed Duration: 700 ms     Memory Size: 128 MB Max Memory Used: 81 MB  
RequestId: 97e615c4-5e42-457e-8c8a-02e7001957f5 Process exited before completing request

根据它看起来libfontconfig.so.1缺少的错误,但我找不到任何方法将其添加到依赖项中。

标签: lambdahighcharts

解决方案


为此,我创建了一个 Github 存储库,其中包含一个专门用于将 Highcharts 导出服务器部署到 Lamdba 的专用项目。

Github:https ://github.com/tarkal/highchart-lambda-export-server

我已经为那些希望从头开始构建的人提供了详细的说明,以及可以直接上传到 Lambda 的预构建 zip。

该项目包含对评论中提到的缺失字体的修复。


推荐阅读