skip to Main Content

I have a dockerized nodejs application. I use libreoffice for converting to PDF.

How can I add libreoffice to dockerfile with nodejs app? And how can I make the app be able to use the libreoffice app?

FROM node:16.8.0-alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN apk add --update python3 make g++ && rm -rf /var/cache/apk/*

RUN npm install

# If you are building your code for production
#RUN npm ci --only=production

# Bundle app source
COPY . .

RUN npm run build

EXPOSE 80:80

CMD [ "node", "build/index.js","STAGING"]

2

Answers


  1. Adding the following to your Dockerfile should install libreoffice

    RUN apk add libreoffice
    

    https://pkgs.alpinelinux.org/package/edge/community/x86/libreoffice

    Login or Signup to reply.
  2. Adding the following line installs LibreOffice

    RUN apk add libreoffice
    

    It is also necessary to install Java Runtime Environment (JRE) in the container, which is required for LibreOffice to function correctly. Adding the following line installs the OpenJDK 8 JRE package on Alpine Linux.

    RUN apk add openjdk8-jre
    

    It is possible to run LibreOffice commands in a Node.js application without installing any additional libraries. You can use the child_process module in Node.js to execute any shell command. The child_process module provides several methods to spawn new child processes, which can be used to execute shell commands. In the following code, I have used the exec method to run the LibreOffice command.

    // Importing the child_process module from the Node.js standard library
    const { exec } = require("child_process");
    
    // Importing the util module from the Node.js standard library
    const util = require("util");
    
    // Converting the exec function from a callback-based API to a Promise-based API
    const execProm = util.promisify(exec);
    
    // Defining a function to run a command using the execProm function
    const runCommand = async (command) => {
      try {
        // Running the command using the execProm function and waiting for it to complete
        await execProm(command);
      } catch (ex) {
        // Handling any errors that occur while running the command
        console.error(ex);
      }
    };
    
    // Defining the output directory path
    const outputPath = "./output/";
    
    // Defining the input file path
    const inputFilePath = "./file.pdf";
    
    // Defining the command to be executed
    const command = `soffice --infilter="impress_pdf_import" --convert-to pptx:"Impress MS PowerPoint 2007 XML" --outdir ${outputPath} ${inputFilePath}`;
    
    // Defining a handler function to run the command
    const handler = async () => {
      // Calling the runCommand function to execute the command
      await runCommand(command);
    };
    
    // Calling the handler function to start the command execution
    handler();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search