node-html-pdf: Getting errors using this package in AWS Lambda Nodejs 10.x and up

I’ve been using this package for ages and worked like a charm on AWS Lambda with Nodejs 8.10 and older.

But AWS now forces you to use 10.x and later…

When I updated my version I started getting the following errors from the lambda execution stack:

ERROR	Uncaught Exception	
{
    "errorType": "Error",
    "errorMessage": "write EPIPE",
    "code": "EPIPE",
    "errno": "EPIPE",
    "syscall": "write",
    "stack": [
        "Error: write EPIPE",
        "    at WriteWrap.afterWrite [as oncomplete] (net.js:789:14)"
    ]
}

It also throws this error just before the above error:

Error: html-pdf: Received the exit code '127'
/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

    at ChildProcess.respond (/var/task/node_modules/html-pdf/lib/pdf.js:121:31)
    at ChildProcess.emit (events.js:198:13)
    at ChildProcess.EventEmitter.emit (domain.js:448:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)

I have tried setting the phantomPath option, still did not help.

Any help will be greatly appreciated.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 11
  • Comments: 24

Most upvoted comments

Two other resources got this fixed for me on Node 10:

For more context and a guide if you want to compile everything yourself: https://stackoverflow.com/questions/56795567/how-do-you-install-phantomjs-on-aws-lambda

Or get the output in this repo: https://github.com/naeemshaikh27/phantom-lambda-fontconfig-pack

As a minimum to get it working:

  • Copy all the .so files and put them in your project root
  • Copy the fonts.conf file and put in fonts directory
  • Add Lambda Environment Variable “FONTCONFIG_PATH” with value equal to fonts directory (so “/var/task/{FONTS_DIR}”)

NOTE: LD_LIBRARY_PATH was not required for it to work

I am also facing the same issue on Microsoft azure app server “error”: { “status”: 500, “message”: “html-pdf: Received the exit code ‘127’\n/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\n” }

I solved this issue following the steps of @jeff-shep but then my pdf was generated without any text so I had to change the content of fonts.config

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

https://forums.aws.amazon.com/thread.jspa?messageID=776307