简体   繁体   English

AWS Lambda 中的 PhantomJS(缺少 libfontconfig)

[英]PhantomJS in AWS Lambda (Missing libfontconfig)

I am trying to get my lambda function to use phantomjs, however it keeps running into an error of a missing dependency: libfontconfig / fontconfig.我试图让我的 lambda 函数使用 phantomjs,但是它一直遇到缺少依赖项的错误:libfontconfig / fontconfig。 On my Centos VPS when I install fontconfig (dnf install fontconfig -y) my code works fine.在我的 Centos VPS 上,当我安装 fontconfig (dnf install fontconfig -y) 时,我的代码工作正常。 However when running on lambda I cannot figure out how to get this library to be ran with my function.但是,在 lambda 上运行时,我无法弄清楚如何让这个库与我的函数一起运行。

This is my code: (Attempting to get the best selling products through the AliExpress package, which uses phantomjs)这是我的代码:(尝试通过使用phantomjs的AliExpress包获得最畅销的产品)

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);
    }
});

I am expecting the result to be an array of AliExpress URLs, this is what happens whenever I run it with fontconfig installed on my VPS.我期望结果是一个 AliExpress URL 数组,每当我在 VPS 上安装了 fontconfig 的情况下运行它时都会发生这种情况。 However, on my lambda function AND on my VPS without fontconfig installed I am getting this error:但是,在我的 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

I believe I now either have to 1) Figure out how to run phantomjs without this dependency or 2) Figure out how to install this dependency to my Lambda function's "server"我相信我现在要么必须 1) 弄清楚如何在没有此依赖项的情况下运行 phantomjs 或 2) 弄清楚如何将此依赖项安装到我的 Lambda 函数的“服务器”

Perhaps there's a previous version of phantomjs that gives me the functionality I want without this dependency?也许有一个以前版本的 phantomjs 可以在没有这种依赖性的情况下为我提供我想要的功能? Not sure不确定

Anyone know how I can solve this problem?有谁知道我该如何解决这个问题? Thank you!谢谢!

Here's what I just got to work for custom fonts on AWS Lambda with pandoc/xelatex.这是我刚刚使用 pandoc/xelatex 在 AWS Lambda 上为自定义字体工作的内容。 I'd bet you can adapt it to do something similar for your use case.我敢打赌你可以调整它为你的用例做类似的事情。

I created a fonts directory in my project and placed all of my fonts there.我在我的项目中创建了一个fonts目录并将我所有的字体放在那里。 Also in that directory I created a fonts.conf file that looks like this:同样在该目录中,我创建了一个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>

And then in my (node.js based) handler function before shelling out to call pandoc I set an ENV var to tell fontconfig where to find the fonts.然后在我的(基于 node.js 的)处理函数中,在调用 pandoc 之前,我设置了一个 ENV 变量来告诉 fontconfig 在哪里可以找到字体。

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

After doing that I can refer to a font, like Bitter , in my template by name (just Bitter ) and then pandoc/xelatex/fontconfig/whatever knows which version of the font to use (like Bitter-Bold.otf vs Bitter-Italic.otf ) based on the styling that any bit of text is supposed to have.这样做之后,我可以在我的模板中通过名称(只是Bitter )引用一种字体,如Bitter ,然后 pandoc/xelatex/fontconfig/whatever 知道要使用哪个版本的字体(如Bitter-Bold.otfBitter-Italic.otf )基于任何文本应该具有的样式。

I figured this out based on the tips in this project for getting RSVG to work with custom fonts on Lambda: https://github.com/claudiajs/rsvg-convert-aws-lambda-binary/blob/master/README.md#using-custom-fonts我根据这个项目中的提示,让 RSVG 与 Lambda 上的自定义字体一起工作: https : //github.com/claudiajs/rsvg-convert-aws-lambda-binary/blob/master/README.md#使用自定义字体

For those ending up there, here's my little story: I wanted to use a Roboto font to generate html-template-based PDF with phantomJs on a AWS Lambda execution.对于那些在那里结束的人,这是我的小故事:我想使用 Roboto 字体在 AWS Lambda 执行上使用 phantomJs 生成基于 html 模板的 PDF。

My application 'App' is in nodeJs and depends on a 'RunTimeDep' dependency which executes a bunch of useful stuff for App at runtime.我的应用程序“App”在 nodeJs 中,依赖于“RunTimeDep”依赖项,它在运行时为 App 执行一堆有用的东西。 Among those tasks, it generates a PDF based on a html template, in which I wanted to use Roboto Mono.在这些任务中,它基于 html 模板生成一个 PDF,我想在其中使用 Roboto Mono。 Just to mention, the html template is in 'App'.顺便提一下,html 模板在“App”中。

To do so, I created the /fonts folder as @Jeremy Green mentionned in the 'RunTimeDep' module.为此,我创建了 /fonts 文件夹,如“RunTimeDep”模块中提到的@Jeremy Green。 I converted the Roboto fonts into PFB format, and put them there.我将 Roboto 字体转换为 PFB 格式,并将它们放在那里。 I added thus this fonts.conf:我因此添加了这个fonts.conf:

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

Finally, I added the FONTCONFIG_PATH to point to /var/task/node_modules/RunTimeDep/fonts/ (I used serverless envrionment variable, but process.env should be fine too)最后,我添加了 FONTCONFIG_PATH 以指向 /var/task/node_modules/RunTimeDep/fonts/(我使用了无服务器环境变量,但 process.env 也应该没问题)

And this actually worked just great这实际上很好用

In your console you may type:在您的控制台中,您可以输入:

$ sudo yum install fontconfig

This will install the required library.这将安装所需的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM