NickNaso / ghostscript4js

Ghostscript4JS binds the Ghostscript C API to the Node.JS world.
http://www.nacios.it
Apache License 2.0
67 stars 19 forks source link

Cannot find the libgs file during runtime in serverless environment (AWS Lambda) #65

Open kaldebert opened 3 years ago

kaldebert commented 3 years ago

Is it possible to use ghostscript4js and ghostscript in a serverless environment as AWS Lambda (and Layers)?

I tried to install both ghostscript4js and ghostscript from a Dockerfile to be uploaded in a AWS Layer (Lambda).

FROM lambci/lambda-base-2:build

# Replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

ENV GS_TAG=gs952
ENV GS_VERSION=9.52
ENV NVM_VERSION=0.38.0
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 12.13.0

RUN yum install -y wget

# Install Ghostscript
RUN mkdir /usr/local/src/ghostscript && \
  cd /usr/local/src/ghostscript && \
  wget -qO - https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/$GS_TAG/ghostscript-$GS_VERSION.tar.gz | tar -zxf - && \
  cd ghostscript-$GS_VERSION && \
  ./configure --without-luratech && \
  make so

# Copy Ghostscript to be used by Ghostscript4JS
RUN mkdir -p /opt/ghostscript/bin
RUN mv /usr/local/src/ghostscript/ghostscript-$GS_VERSION/sobin/* /opt/ghostscript/bin

# Install nvm
RUN mkdir /usr/local/nvm && \
  wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v$NVM_VERSION/install.sh | bash

# Install node and npm
RUN source $NVM_DIR/nvm.sh && \
  nvm install $NODE_VERSION && \
  nvm alias default $NODE_VERSION && \
  nvm use default

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH

# Install Ghostscript4JS
RUN mkdir /opt/nodejs && \
  cd /opt/nodejs && \
  npm init -y && \
  npm install node-gyp && \
  npm install ghostscript4js --GS4JS_HOME="/opt/ghostscript/bin"

# Create the ZIP file
RUN cd /opt && zip -r /tmp/gs.zip ghostscript nodejs

But when the AWS Lambda is called, I got this error:

Error: libgs.so.9: cannot open shared object file: No such file or directory
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1057:18)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at bindings (/opt/nodejs/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/opt/nodejs/node_modules/ghostscript4js/lib/ghostscript4js.js:25:31)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)

Where should be the libgs file during the runtime? In the GS4JS_HOME folder or is it copied in another folder during ghostscript4js'installation?

NickNaso commented 3 years ago

Hi @kaldebert, it search the library libgs.so in the folder you set using the evn variable GS4JS_HOME. When the module is loaded it needs to load the shared library libgs.so otherwise you get the error you reported, An idea could be to copy the library in the folder of your project and the try to install it. Es. The folder for your project is my-project and you copy the content of the folder /usr/local/src/ghostscript/ghostscript-$GS_VERSION/sobin/* to my-project/gs The you can install using the following command: npm install ghostscript4js --GS4JS_HOME=../../../gs I hope that this help you.

NickNaso commented 2 months ago

This issue is connected to https://github.com/NickNaso/ghostscript4js/issues/61