Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS lambda html-pdf phantomjs "Error: write EPIPE" on NodeJS 12.x

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)"]}
like image 516
Manmeet Bhurjee Avatar asked Oct 16 '22 02:10

Manmeet Bhurjee


1 Answers

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

like image 121
Renish Gotecha Avatar answered Oct 20 '22 17:10

Renish Gotecha