5

I have been using html-pdf (phantomjs) for creating PDF on AWS Lambda with NodeJS 8.0 and it was working fine. Since AWS Lambda has stopped support on NodeJS 8.0 we have updated our NodeJS version to the latest 12.x and we are now getting the following error when we run out PDF Lambda function:

{"errorType":"Error","errorMessage":"write EPIPE","code":"EPIPE","errno":"EPIPE","syscall":"write","stack":["Error: write EPIPE"," at afterWriteDispatched (internal/stream_base_commons.js:154:25)"," at writeGeneric (internal/stream_base_commons.js:145:3)"," at Socket._writeGeneric (net.js:784:11)"," at Socket._write (net.js:796:8)"," at doWrite (_stream_writable.js:403:12)"," at writeOrBuffer (_stream_writable.js:387:5)"," at Socket.Writable.write (_stream_writable.js:318:11)"," at PDF.PdfExec [as exec] (/var/task/node_modules/html-pdf/lib/pdf.js:141:15)"," at PDF.PdfToBuffer [as toBuffer] (/var/task/node_modules/html-pdf/lib/pdf.js:44:8)"," at /var/task/index.js:121:38"]}

phantomPath: './phantomjs_lambda/phantomjs'
process.env.FONTCONFIG_PATH='/var/task/fonts'

I tried to check similar issues in StackOverflow. I have pointed the phantomjs path correctly, also have the fontconfig in place. We are now stuck as we are unable to update the Lambda function. Any help will be highly appreciated.

Update: Changed the path to phantomjs binary:

phantomPath: './node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs'

After changing the path to phantomjs binary, the error has changed to:

{"errorType":"Error","errorMessage":"write EPIPE","code":"EPIPE","errno":"EPIPE","syscall":"write","stack":["Error: write EPIPE","    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:92:16)"]}
Manmeet Bhurjee
  • 143
  • 1
  • 10

3 Answers3

3

I recently face this issue as well, when i have update Node version of lambda to 14.x. After following below steps i can able to resolved my issue.

I passed phantomPath : "./node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs" at pdf.create(html,tempParamms)

  • get all files from this Repo: https://github.com/naeemshaikh27/phantom-lambda-fontconfig-pack
  • Copy all .so files and paste them in your project root.
  • Create a directory called fonts in your project root.
  • Copy all files apart from .so files and paste all files in newly created fonts directory.
  • Replace below code from file fonts/fonts.conf.
  • Add Lambda Environment Variable FONTCONFIG_PATH with value = /var/task/fonts
  • Upload code to 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>
   <match target="font">
  <edit mode="assign" name="rgba">
   <const>rgb</const>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="hinting">
   <bool>true</bool>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="hintstyle">
   <const>hintslight</const>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="antialias">
   <bool>true</bool>
  </edit>
 </match>
 <match target="font">
  <edit mode="assign" name="lcdfilter">
   <const>lcddefault</const>
  </edit>
 </match>
</fontconfig>

Ref : Link

Renish Gotecha
  • 2,232
  • 22
  • 21
0

You are getting this Error because AWS pull out basic utilities in the environment from node 10.x to 12.x (eg ImageMagick).

The solution is to either include the missing functionality your self or use lambda layers. Here is information on lambda layers with ImageMagic https://github.com/serverlesspub/imagemagick-aws-lambda-2

For further reading on this problem I suggest looking into the AWS forum here: https://forums.aws.amazon.com/thread.jspa?threadID=305691

Peter Savnik
  • 763
  • 1
  • 8
  • 27
0

html-pdf use a npm package phantomjs-prebuilt

phantomjs-prebuilt will automatically install phantomjs for you based on your OS/Archritecture

It's important that the node_modules doesn't get copied over to the docker image And rather get installed on the docker image itself

I have created custom docker image for azure, for AWS you would need to do the same https://docs.aws.amazon.com/lambda/latest/dg/images-create.html

Following the this thread you would need to install a couple of dependencies as well https://gist.github.com/julionc/7476620

Ensure you have node_modules/ in your .dockerignore file

node_modules/

Docker file for azure functions (Should create simular dockerfile for AWS lambdas)

FROM mcr.microsoft.com/azure-functions/node:3.0-node12-appservice

RUN apt-get update
RUN apt-get install build-essential chrpath libssl-dev libxft-dev -y
RUN apt-get install libfreetype6 libfreetype6-dev -y
RUN apt-get install libfontconfig1 libfontconfig1-dev -y

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    OPENSSL_CONF=/etc/ssl/

COPY . /home/site/wwwroot


RUN cd /home/site/wwwroot && \
    npm install

Adding the OPENSS_CONF=/etc/ssl/ is essential as phantomjs will throw another error

Quinn Keaveney
  • 1,248
  • 1
  • 17
  • 39