我在Google Cloud Run上部署了一个NestJS应用程序,它使用puppeteer(V7.0.1)生成PDF。在本地,一切都运行得非常好,但在我的Cloud Run Service上,我不断收到以下错误:/usr/app/node_modules/puppeteer/.local-chromium/linux-848005/chrome-linux/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory
该目录确实存在于Docker容器中(使用RUN ls node_modules/.....
检查)。使用来自puppeteer troubleshooting doc的Docker代码或我在Web上发现的任何其他类似问题的代码片段都会导致同样的错误。
Dockerfile:
FROM node:12-slim AS base
WORKDIR /usr/app
FROM base AS build
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
COPY package.json yarn.lock ./
RUN yarn --prod
COPY . ./
RUN yarn add @nestjs/cli
RUN yarn sass && yarn build && yarn copy-pdf-assets
FROM base
COPY --from=build /usr/app ./
ENV PATH /usr/app/node_modules/.bin:$PATH
CMD yarn start:prod
生成PDF函数:
//also tried headless: true or false and many other flags that deemed to fix it for others
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
try {
const page = await browser.newPage();
await page.setViewport({ height: 792, width: 1039 })
await page.setContent(this.getTemplate(template, data), {
waitUntil: ['load', 'domcontentloaded', 'networkidle0']
});
await page.addStyleTag({ path: this.resolvePath(`/templates/${template}/styles.css`) })
await new Promise(resolve => setTimeout(resolve, 500));
return await page.pdf({ format: 'a4', landscape: true, printBackground: true });
} finally {
await browser.close();
}
如果你需要其他信息就告诉我。先谢谢你了。
1条答案
按热度按时间brc7rcf01#
对于任何阅读到类似问题和设置的人:我的错误是我的docker文件中的构建步骤。因为我在那里安装了chromium,而不是实际的运行步骤,puppeteer在运行时实际上没有chromium示例。我只是把apt-get的东西移到了我的docker文件的最后一步,这就解决了它。
编辑2023-05-23:
上述dockerfile的工作版本: