Still have questions?
Contact support

Server-Side Rendering

Overview

Server-side rendering is a tool that helps a lot in some cases: for example, when you've got an automated email system, and you need to include charts in letters; when you need charts in reports, which are generated on servers; when you need to show the charts on a Smart TV with a stripped version of a browser; etc. In such cases we offer you to use AnyChart Export Server.

AnyChart Export server is also used to provide Export chart to CSV, Excel, JSON and XML options.

Environment

AnyChart Export Server uses PhantomJS which emulates a browser on the server (WebKit), runs our charts in it, gets SVG and converts it into *.PNG, *.JPG or *.PDF files, using Apache Batik. Export to Excel uses Apache POI. Exporting to CSV, JSON and XML doesn't require PhantomJS, server serves only as an intermediary to allow file to be saved using a browser. AnyChart Export Server itself is a jar-file that runs using Java so it works Windows, Linux, MacOS or any other OS where Java is available.

To run the AnyChart Export Server, do the following:

Command Line Usage

If you want to use the AnyChart Export Server from the Command Line mode you have to set "cmd" (Command Line) as the first parameter, then define the path to the chart or insert the chart code as a string and then set the parameters of the image (dimensions, quality, extension and so on). You'll find the full list of parameters below.

Sample command line:

java -jar anychart-export.jar cmd --script "var chart = anychart.line([1,2,5]); chart.container('container'); chart.draw();" --output-path YOUR_OUT_PATH

Full list of the parameters available:

NameOptionsDescription
–helpShows help
–scriptThe chart code
–input-fileSet the path to the file with the chart code (script)
–output-fileName for the output file (e.g.: "Chart1" or "Chart0.png")
–output-pathSet the path for the folder where the chart will be saved
–typesvg, png, jpg, pdf, csv, xlsx, xml, jsonThe file type
–container-idThe id for the container which will be generated in PhantomJS for the chart
–container-widthThe container width. The image quality will be better if the container is larger than the image.
–container-heightThe container height. The image quality will be better if the container is larger than the image.
–image-widthThe image width. Doesn't work for *.pdf
–image-heightThe image height. Doesn't work for *.pdf
–force-transparent-whiteIf the chart's background is originally transparent - makes it white
–jpg-qualityfrom 0 to 1The image quality
–pdf-sizeDefine the *.pdf document paper size
–pdf-xX-coordinate of the chart in the *.pdf document (in pixels)
–pdf-yY-coordinate of the chart in the *.pdf document (in pixels)
–pdf-width*.pdf document height (in pixels)
–pdf-height*.pdf document height (in pixels)
–pdf-landscapePortrait or LandscapeThe *.pdf document layout

Web Server Usage

AnyChart Export Server is also used when you use AnyChart Export methods and by default AnyChart component uses server hosted at https://www.anychart.com/. Although we do our best to keep AnyChart site up and running 24x7x365 - we do not guarantee export server availability. If you want to have full control over the ability of the component to export images, or just don't want to use AnyChart Server due to the security or accessibility concerns - just run Export Server in server mode on the server you control and trust.

To run Export server in server mode set "server" as the first parameter. Host and port params are optional. The usual http web server is run, it recieves POST requests and sends the result as a base64-line or as a Byte Array with the "File Transfer" header.

When you stop the server, you must stop the PhantomJS process too.

The sample of server running:

java -jar anychart-export.jar server

The sample of a command written in console:

curl -X POST -H "Accept: application/json" --data "responseType=base64&dataType=script&data=var chart = anychart.line([1,2,5]); chart.container('container'); chart.draw();" localhost:2000/png

Full list of server parameters that can be set:

NameOptionsDescription
–hostThe host where to run the server
–portThe port where to run the server
–allow-scripts-executingy (yes), n (no)A boolean parameter. If set in Y (e.g. as we do with https://export.anychart.com/) it might affect the security, so there's a flag which is N by default.

There's a list of URL's which export server responds to:

  • /status
  • /png
  • /jpg
  • /pdf
  • /svg
  • /csv
  • /xlsx
  • /xml
  • /json

Request parameters (required):

  • data - script or svg that should be transformed into a picture.
  • data-type - a field that contains the information about the data, it might be "script" or "svg".
  • response-type - a field that tells how to export the result (file or as base64)

Optional request parameters:

  • file-name - file name
  • width - picture width
  • height - picture height
  • quality - picture quality
  • force-transparent-white - make the background white or leave it transparent
  • pdf-size - the *.pdf-document sheet size
  • pdf-x - x of the chart in the *.pdf document
  • pdf-y - y of the chart in the *.pdf document
  • pdf-width - pdf width
  • pdf-height - pdf height
  • landscape - the document orientation

How to Set the Path to PhantomJS

As it was mentioned before, export server needs PhantomJS. If you have installed it somewhere different from the default or you've got Windows OS, check the place where Phantom JS is installed and set the right path for the export server. Like this:

java -Dphantomjs.binary.path=PATH_TO_YOUR_PHANTOMJS -jar

How to Set the Path to Export Server

If you have decided to use your own server, use the anychart.server() method and set the address of your server as a parameter:

anychart.server("http://localhost:2000");

Contribution

If you've got any suggestions or ideas about export server work and improvements, welcome to our open repository on github.