3

I try to install libreoffice to docker image of lambda function. When I run it on local machine it work fine but when deployed the image to lambda function, It keeps saying "failed to read path from javaldx" and can not open soffice. Are there any solution to fix this? Here is DockerFile:

FROM public.ecr.aws/lambda/nodejs:12

RUN yum install wget tar -y
RUN yum install java-1.8.0-openjdk-devel -y
RUN mkdir "LibreOffice_7.2_Linux_x86-64_rpm"
ADD LibreOffice_*_Linux_x86-64_rpm ./LibreOffice_7.2_Linux_x86-64_rpm
RUN ls -la
RUN cd  LibreOffice_*_Linux_x86-64_rpm && cd RPMS && yum -y localinstall *.rpm
RUN chmod -R 777 /opt/libreoffice7.2
ADD test.docx ./
RUN export HOME=/tmp

COPY index.js package*.json ./
RUN npm install

CMD [ "index.handler" ]

Here is my handler code:

/* Amplify Params - DO NOT EDIT
    ENV
    REGION
    STORAGE_DOCBACKENDD3C9A483_BUCKETNAME
Amplify Params - DO NOT EDIT */

const {writeFileSync, readFileSync} = require('fs');
const {execSync} = require('child_process');
const {S3} = require('aws-sdk');

const s3 = new S3(
    {
        params: {
            Bucket: "documentgenerationb25c0536d4054b9faeafca747e3f295559-dev",
            apiVersion: '2006-03-01'
        }
    }
);
const convertCommand = `/opt/libreoffice7.2/program/soffice --convert-to pdf test.docx --headless`;

exports.handler = async () => {
    const filename = "test.docx"
    const {Body: inputFileBuffer} = await s3.getObject({Key: `public/${filename}`}).promise();
    writeFileSync(`/tmp/${filename}`, inputFileBuffer);

    execSync(`${convertCommand} ${filename}`);
    const outputFilename = `test1-brandon.pdf`;
    const outputFileBuffer = readFileSync(`/tmp/${outputFilename}`);

    await s3
    .putObject({
        Key: `public/${outputFilename}`, Body: outputFileBuffer,
        ACL: 'public-read', ContentType: 'application/pdf'
    })
    .promise();

    return `https://documentgenerationb25c0536d4054b9faeafca747e3f295559-dev.s3.amazonaws.com/public/${outputFilename}`;
};

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

2 Answers2

1

I am currently using this https://github.com/shelfio/aws-lambda-libreoffice with a lambda layer and you only need to install @shelf/aws-lambda-libreoffice. So it is really only like 4 lines of code with serverless deployment.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jan Sila
  • 1,554
  • 3
  • 17
  • 36
  • 1
    if you haven't used it, do not recommend it to people. Using the library you mentioned, will convert the files on average in 16 seconds, on each lambda COLD_START. On every COLD_START you'll get the conversion done in 16 seconds, this is crazy to recommend something like that to people – Ion Scorobogaci Jun 18 '22 at 15:06
0

Looks like this answer Warning: failed to read path from javaldx applies to your case too.

Your user doesn't has a home folder set, or the home folder is not writeable. I just switched from calling libreoffice directly to calling it via a shell script

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Marcello Romani
  • 2,967
  • 31
  • 40