node.js - AWS Lambda 中的 PhantomJS(缺少 libfontconfig)

标签 node.js amazon-web-services lambda npm phantomjs

我试图让我的 lambda 函数使用 phantomjs,但是它一直遇到缺少依赖项的错误:libfontconfig/fontconfig。在我的 Centos VPS 上,当我安装 fontconfig (dnf install fontconfig -y) 时,我的代码工作正常。但是,在 lambda 上运行时,我不知道如何让这个库与我的函数一起运行。

这是我的代码:(试图通过使用phantomjs的全局速卖通包裹获得最畅销的产品)

const aliExpress = require('aliexpress');

exports.handler = (event, context, callback) => {
    console.log('Handler ran!');
    aliExpress.BestSelling.get().then((goods) => {
        console.log('Found results!');
        const urls = [];
        for(let index in goods) {
            const url = goods[index].url;
            urls.push(url);
        }
        console.log('Returning URLs:');
        console.log(urls);
        callback(null, urls);
    }).catch((err) => {
        console.log('Error:');
        console.log(err);
        callback(err);
    });
};

// For testing on VPS
exports.handler(null, null, (err, result) => {
    if(err) {
        console.log('Err:');
        console.log(err);
    } else {
        console.log('Result:');
        console.log(result);
    }
});

我期望结果是一组 AliExpress URL,每当我在 VPS 上安装 fontconfig 时运行它都会发生这种情况。但是,在我的 lambda 函数和未安装 fontconfig 的 VPS 上,我收到此错误:

Handler ran!
(node:1966) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error reading from stdin: Error: write EPIPE
(node:1966) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
error: /home/function/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

我相信我现在必须 1) 弄清楚如何在没有此依赖项的情况下运行 phantomjs 或 2) 弄清楚如何将此依赖项安装到我的 Lambda 函数的“服务器”

也许有一个以前版本的 phantomjs 可以在没有这种依赖性的情况下为我提供我想要的功能?不确定

有人知道怎么解决这个问题吗?谢谢!

最佳答案

这是我刚刚使用 pandoc/xelatex 在 AWS Lambda 上处理自定义字体的内容。我敢打赌,您可以调整它来为您的用例做类似的事情。

我在我的项目中创建了一个 fonts 目录,并将我所有的字体都放在那里。同样在该目录中,我创建了一个如下所示的 fonts.conf 文件:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>

然后在我的(基于 node.js 的)处理程序函数中,在调用 pandoc 之前,我设置了一个 ENV var 来告诉 fontconfig 在哪里可以找到字体。

process.env.FONTCONFIG_PATH='/var/task/fonts'

这样做之后,我可以在我的模板中按名称引用字体,如 Bitter(只是 Bitter),然后是 pandoc/xelatex/fontconfig/whatever knows which要使用的字体版本(如 Bitter-Bold.otfBitter-Italic.otf)基于任何文本应该具有的样式。

我根据这个项目中的提示解决了这个问题,让 RSVG 在 Lambda 上使用自定义字体:https://github.com/claudiajs/rsvg-convert-aws-lambda-binary/blob/master/README.md#using-custom-fonts

关于node.js - AWS Lambda 中的 PhantomJS(缺少 libfontconfig),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603687/

相关文章:

Python/Pandas Dataframe - 当 0 时将小数精度替换为 int 值,否则四舍五入到 n 位小数

node.js - Amazon EC2 Linux 上的 NPM 安装错误 MEAN.js 示例应用程序?

amazon-web-services - 启动 StepFunction 并退出不会触发执行

c++ - for 循环与带有 lambda 的 std::for_each

node.js - AWS同步lambda与nodejs的lambda调用

amazon-web-services - API Gateway VPC 链接集成到 NLB 提供 404

node.js - MongoDB 架构 : store id as FK or whole document

node.js - mongodb 和 geoWithin

node.js - Gulp任务child_process.spawncurl错误

没有 HTTP_REFERER 的 Django + CloudFront 缓存