我有一个在 Laradock 上运行的 Laravel 项目,我正在尝试使用这个包从视图中生成 PDF 文件:https ://github.com/spatie/browsershot
它使用 Puppeteer 来完成该部分并使用无头镀铬。
至此,我刚刚制作了一个控制器,它调用了一个基本函数进行测试:
$image = Browsershot::url('https://example.com')
->noSandbox()
->screenshot();
我得到错误:
Error: Failed to launch the browser process! /var/www/projects/my-project/node_modules/puppeteer/.local-chromium/linux-856583/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
我确实通过 npm 在本地安装了 puppeteer 并且/var/www/projects/my-project/node_modules/puppeteer/.local-chromium/linux-856583/chrome-linux/chrome
确实存在。
我不确定该怎么做。在 Browsershot 文档中,它说您可以运行:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev
sudo npm install --global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium
在锻造。我不确定这是否也适用于我的 laradock 环境。但是错误是抱怨它在我的文件夹中找不到该文件node_modules
。
完整的原始错误是:
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node '/var/www/projects/my-project/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"https:\/\/example.com","action":"screenshot","options":{"type":"png","args":["--no-sandbox"],"viewport":{"width":800,"height":600}}}'" failed. Exit Code: 1(General error) Working directory: /var/www/projects/my-project/public Output: ================ Error Output: ================ Error: Failed to launch the browser process! /var/www/projects/my-project/node_modules/puppeteer/.local-chromium/linux-856583/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md at onClose (/var/www/projects/my-project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20) at Interface.<anonymous> (/var/www/projects/my-project/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:183:68) at Interface.emit (node:events:390:22) at Interface.close (node:readline:514:8) at Socket.onend (node:readline:248:10) at Socket.emit (node:events:390:22) at endReadableNT (node:internal/streams/readable:1307:12) at processTicksAndRejections (node:internal/process/task_queues:81:21)
编辑:在我的 Dockerfile 中添加了以下内容php-fpm
RUN 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-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \
&& apt-get install -y nodejs \
&& npm install --global --unsafe-perm puppeteer \
&& chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium
该/usr/lib/node_modules/
文件夹从未创建,现在当我尝试加载页面时,出现503
错误。