PhantomJS Installation
PhantomJS is a WebKit-based JavaScript API. It uses QtWebKit as its core browser functionality and WebKit to compile and execute JavaScript code. It can do anything you can do in a WebKit-based browser, such as server-side generation of JS chart images, with a wide range of application scenarios.
Here’s a brief explanation of how to install it.
Linux
$ yum install -y bzip2
$ bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2
$ tar xvf phantomjs-2.1.1-linux-x86_64.tar
$ mv -r phantomjs-2.1.1-linux-x86_64 /usr/local/
$ cd /usr/local/
$ mv phantomjs-2.1.1-linux-x86_64/ phantomjs
$ ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin
Font Issues
In practical usage, I used PhantomJS to execute Highcharts chart generation for images, and the charts used Chinese fonts, so it was necessary to install Microsoft YaHei font. The specific operation commands are as follows:
$ mkdir /usr/share/fonts/chinese
$ cp MicrosoftYaHei.ttf /usr/share/fonts/chinese/
$ mkfontscale
$ mkfontdir
$ fc-cache -fv
# List Chinese fonts
$ fc-list:lang=zh
Mac
brew install phantomjs
Using brew for installation is the most convenient method
Windows
Download the zip package and extract it, for example, place it in D://phantomjs-2.1.1-windows
, then configure environment variables. In the system environment variables, add ;D:\phantomjs-2.1.1-windows\bin
to the Path variable.
Final Thoughts
PhantomJS provides a powerful headless browser solution for various automation and testing scenarios. The installation process varies by operating system, with Homebrew offering the simplest method on macOS, while Linux and Windows require manual setup. When working with Chinese fonts or other non-standard character sets, remember to install the necessary font packages and configure the font cache properly. These installation steps should help you get PhantomJS up and running efficiently across different platforms.