我有一个 dockerized nodejs 应用程序。我使用 libreoffice 转换为 PDF。
如何使用 nodejs 应用程序将 libreoffice 添加到 dockerfile?我怎样才能让应用程序能够使用 libreoffice 应用程序?
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"]
将以下内容添加到您的 Dockerfile 应该安装 libreoffice
RUN apk add libreoffice
https://pkgs.alpinelinux.org/package/edge/community/x86/libreoffice
问题: 如何使用 nodejs 应用程序将 libreoffice 添加到 dockerfile?
答案: 添加以下行将在容器上安装 LibreOffice,这是一个开源办公生产力套件,可用于操作各种类型的文档
RUN apk add libreoffice
在 Docker 容器中运行 LibreOffice 时,需要安装 Java Runtime Environment (JRE),以确保需要 Java Runtime Environment (JRE) 的 LibreOffice 应用程序能够正常运行。 JRE 的最低要求版本是 1.8。添加以下行会在 Alpine Linux 上安装 OpenJDK 8 JRE 包。
RUN apk add openjdk8-jre
问题: 我怎样才能让应用程序能够使用 libreoffice 应用程序?
答案:
可以在 Node.js 应用程序中运行 LibreOffice 命令,而无需安装任何额外的库。您可以使用 Node.js 中的 child_process 模块来执行任何 shell 命令。
child_process 模块提供了几种生成新子进程的方法,可用于执行 shell 命令。在下面的代码中,我使用了 exec 方法来运行 LibreOffice 命令。
// 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 = "./Documentation.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();