谬误

运行后提醒Error: Failed to launch the browser process!
...
...

排查

github puppeteer/issues:

https://github.com/puppeteer/...

起因是零碎短少一些包,puppeteer的文档中也有阐明:

https://github.com/puppeteer/...

解决

依照文档装置依赖包:

yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y

重启node服务,仍旧报错:

Error: Failed to launch the browser process![0308/163337.932654:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

文档中也有提到:
https://github.com/puppeteer/...

起因是在root权限下关上了Chromium,能够换角色运行node服务,另一种解决形式是,开启Chromium时传入参数:

const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })

puppeteer不倡议这么做,但我这里是测试,无所谓了,生产环境切勿这么做。

重启node服务,截图胜利但中文乱码,起因是,centos7中缺失中文字体的起因。

解决缺失字体

下载安装字体即可:

  1. 装置字体管理工具
    装置fontconfig来治理字体库
yum -y install fontconfig

装置完fontconfig时,在/usr/shared目录就能够看到fonts和fontconfig目录了(之前是没有的)

查看所有字体:fc-list
查看中文字体:fc-list :lang=zh

  1. 创立字体目录并并批改权限

创立字体目录:

mkdir /usr/share/fonts/chinese

批改字体文件的权限,使root用户以外的用户也能够应用:

chmod -R 755 /usr/share/fonts/chinese
  1. COPY字体到/usr/share/fonts/chinese

在 windows 的 C:\Windows\Fonts目录下找到须要的字体, copy 到 /usr/share/fonts目录下,这里增加雅黑和新宋体:msyh.ttc,simsun.ttc

  1. 建设字体索引信息,更新字体缓存
cd /usr/share/fonts/chinesemkfontscale //如果提醒 mkfontscale: command not found,需自行装置 # yum install mkfontscalemkfontdirfc-cache -fv //如果提醒 fc-cache: command not found,则须要装置# yum install fontconfig