Server-Side Rendering
- Overview
- Environment
- Command Line Usage
- Web Server Usage
- How to Set the Path to PhantomJS
- How to Set the Path to Export Server
- Contribution
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:
- Install PhantomJS: instructions and downloads at http://phantomjs.org/)
- Install Java: version above 6.0 - https://java.com/en/download/ )
- Download AnyChart Export Server binary file
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:
Name | Options | Description |
---|---|---|
–help | Shows help | |
–script | The chart code | |
–input-file | Set the path to the file with the chart code (script) | |
–output-file | Name for the output file (e.g.: "Chart1" or "Chart0.png") | |
–output-path | Set the path for the folder where the chart will be saved | |
–type | svg, png, jpg, pdf, csv, xlsx, xml, json | The file type |
–container-id | The id for the container which will be generated in PhantomJS for the chart | |
–container-width | The container width. The image quality will be better if the container is larger than the image. | |
–container-height | The container height. The image quality will be better if the container is larger than the image. | |
–image-width | The image width. Doesn't work for *.pdf | |
–image-height | The image height. Doesn't work for *.pdf | |
–force-transparent-white | If the chart's background is originally transparent - makes it white | |
–jpg-quality | from 0 to 1 | The image quality |
–pdf-size | Define the *.pdf document paper size | |
–pdf-x | X-coordinate of the chart in the *.pdf document (in pixels) | |
–pdf-y | Y-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-landscape | Portrait or Landscape | The *.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:
Name | Options | Description |
---|---|---|
–host | The host where to run the server | |
–port | The port where to run the server | |
–allow-scripts-executing | y (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
- /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.exports.server() method and set the address of your server as a parameter:
anychart.exports.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.