The Issue of the Day Before

在 Linux 上安裝 Puppeteer

-

安裝 nodejs v6.4.0 以上 → 安裝 Puppeteer → 安裝依賴套件與函式庫

Why

隱形斗篷是很多人的幻想,但無頭騎士卻是傳說。

在跟大神許願多次後,Chromev59 之後,支援無介面(headless)的模式,實現了看不見的夢想。 見 Getting Started with Headless Chrome

為了不讓你的瀏覽器像無頭蒼蠅一樣亂飛,Google 還貼心提供 Puppeteer(操偶師) 來幫忙。

What

Puppeteer

Puppeteer 是使用 nodejs 開發一組高階 API ,可以用來的控制 ‵Chrome` 螢幕截圖,創建 PDF, 導航頁面以及獲取頁面信息。

How

安裝 nodejs

版本必須要在 v6.04.0 以上。 參考 …​

安裝 Puppeteer

$ npm install puppeteer
// Chromium downloaded to ./node_modules/puppeteer/.local-chromium/linux-xxxxxx

Puppeteer 同時會安裝一個與之搭配版本的 Chrome。最好不要任意更換。 該 Chrome 程式會放在 node_modules/puppeteer 目錄下的 .local-chromium/linux-xxxxxx/chrome-linux/chrome

安裝依賴套件與函式庫

如果不是安裝在視窗界面下,則你可能要再安裝一些相關函式庫。

使用 ldd 來查閱相關依賴。

$ ldd ./node_modules/puppeteer/.local-chromium/linux-571375/chrome-linux/chrome | grep not
/* maybe
libX11-xcb.so.1 => not found
libXcomposite.so.1 => not found
libXcursor.so.1 => not found
libXdamage.so.1 => not found
libXfixes.so.3 => not found
libXi.so.6 => not found
libXrender.so.1 => not found
libXtst.so.6 => not found
libcups.so.2 => not found
libXss.so.1 => not found
libXrandr.so.2 => not found
libasound.so.2 => not found
libpangocairo-1.0.so.0 => not found
libpango-1.0.so.0 => not found
libcairo.so.2 => not found
libatk-1.0.so.0 => not found
libatk-bridge-2.0.so.0 => not found
libgtk-3.so.0 => not found
libgdk-3.so.0 => not found
libgdk_pixbuf-2.0.so.0 => not found
*/

可能需要的函式庫如下:

libx11-xcb1 - Xlib/XCB interface library
libxcomposite1 - X11 Composite extension library
libxcursor1 - X cursor management library
libxdamage1 - X11 damaged region extension library
libxi6 - X11 Input extension library
libxext6 - X11 miscellaneous extension library
libxtst6 - X11 Testing -- Record extension library
libcups2 - Common UNIX Printing System(tm) - Core library
libxss1 - X11 Screen Saver extension library
libxrandr2 - X11 RandR extension library
libasound2 - shared library for ALSA applications
libpangocairo-1.0-0 - Layout and rendering of internationalized text
libatk1.0-0 - ATK accessibility toolkit
libgtk-3-0 - GTK+ graphical user interface library
libatk-bridge2.0-0 - AT-SPI 2 toolkit bridge - shared library

查出相關依賴函式庫後,再逐一安裝,即可。

$ sudo apt-get install -y libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxext6 libxtst6 libcups2 libxss1 libxrandr2 libpangocairo-1.0-0 libatk1.0-0 libgtk-3-0 libatk-bridge2.0-0

安裝字型

如果你的宣染後的截圖,中文的部分出現亂碼,可能是因為系統上沒有相關的字型。 可以使用 fc-list | grep <font-name> 檢查是否有網頁需要的字型。

如果沒有則找到網頁上宣告需要的字型檔,將之複製到執行系統上並執行 fc-cache -v -f 將字型載入 cache,即可。

e.q. font-family: "微軟正黑體";

$ fc-list | grep 正黑
$ mkdir /usr/share/fonts/truetype/ms-font
$ cp MSJH.TTC /usr/share/fonts/truetype/ms-font
$ fc-cache -v -f /usr/share/fonts/truetype/ms-fonts
$ fc-list | grep 正黑
// /usr/share/fonts/truetype/ms-fonts/MSJH.TTC: Microsoft JhengHei,微軟正黑體:style=Normal,Regular,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
閱讀在雲端