/Snapshot-PhantomJS

java + phantomsjs 后台生成echarts图片

Primary LanguageJavaApache License 2.0Apache-2.0

说明

因为有后台生成echarts的需求,就找到了这个项目Snapshot-PhantomJS

做了部分修改,然后尝试了一下。

大概10s左右生成一张图片,效率上有点低。

改动:

  • 因为phantomsjs输出图片的base64字符串太长,导致程序直接卡住不动。所以修改js文件。先将base64输出为txt文件,返回文件名。然后由后台读取txt生成图片
  • 需要在项目内设置死phantomsjs的工作目录,顺带将执行的js文件也放到了这里。

暂时当成备选方案处理,后续如果再使用再进行修改。

Test plan

  • Local unit testing
  • Docker
  • AWS remote
    • Linux
    • Windows
    • MacOS
  • Integration Test with Snapshot version

Introduction

This library is used to take snapshots of the charts generated by ECharts-Java. Now it supports images in PNG and JPEG formats with pixelRatio control. Base64 is also supported. We plan to support SVG in the near future. (It is still in the testing stage, please file an issue if you spot a bug.).

Prerequisite

To use this library, make sure you've installed phantomjs.

For Mac users using brew,

brew install phantomjs

For AWS Linux users,

sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

sudo mkdir /opt/phantomjs

bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2

sudo tar -xvf phantomjs-2.1.1-linux-x86_64.tar --directory /opt/phantomjs/ --strip-components 1

sudo ln -s /opt/phantomjs/bin/phantomjs /usr/bin/phantomjs

For other users, please refer to the official website of phantomjs for downloading details.

For Mac users, if you encounter the error "phantomjs cannot be opened because the developer cannot be verified.", please refer to the solution here.

Installation

For a maven project, includes the following in your pom.xml

TBC

For a gradle project, includes the following

TBC

Usage

Snapshot.java is providing several APIs to be used, which includes takeSnapshot() and saveSnapshot().

SnapshotSettingsBuilder.java

It constructs an object with the following properties related to metadata of the snapshot.

Required:

  • String fileType: the fileType of the image. Now we support "png" and "jpg".
  • Option option: must have this field if Chart chart is not specified. This is the option object of ECharts.
  • Chart<?,?> chart: must have this field if Option option is not specified. This is the chart object defined in ECharts-Java.

Optional:

  • int delay: the waiting time (in second) of an image to be fully rendered. Since some ECharts have the animation effects, it is suggested to wait for a few seconds before making a snapshot. The default value is 2.
  • double pixelRation: the pixel ration of an image. It is defined here.

takeSnapshot(SnapshotSettingsBuilder settings)

This function takes an object related to the settings of snapshot, and returns a base64 string of the image.

    SnapshotSettingsBuilder builder = new SnapshotSettingsBuilder(option, "png");
    return Snapshot.takeSnapshot(builder);

saveSnapshot(String imageData, String path)

This function takes the base64 string of an image, and saves the image by the suffix specified by path. E.g. if the path is ./test.png, it will store the image as png file. Now only PNG and JPG are supported. Any other file types will lead to a file with plain base64 string of that image. Note that the file type should be consistent with the one used in takeSnapshot.

    SnapshotSettingsBuilder builder = new SnapshotSettingsBuilder(option, "jpg", 1, 2);
    Snapshot.saveSnapshot(Snapshot.takeSnapshot(builder), "./test.jpg");