PhantomJS Installation

This article provides an introduction to PhantomJS installation, including its advantages, implementation details, and related resources. These steps can help improve the efficiency of PhantomJS installation.

May 1, 2018 · 2 min read · 262 Words · -Views -Comments · Digital Life
🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

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.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover