Export AnyChart graph to PDF with wkhtmltopdf

July 24th, 2018 / by api2pdf /


We previously made a post on how to export a HighChart graph to PDF using the wkhtmltopdf endpoint. This time we are going to cover AnyChart, which is arguably better than HighCarts in many areas. In this post we are going to cover printing an AnyChart graph to PDF with wkhtmltopdf.

Sample Code

The following sample code is taken from the AnyChart website demo for a basic Area chart. We are going to use this as the baseline for printing to PDF.

Export to PDF

Using the wkhtmltopdf endpoint from the Api2Pdf documentation, you have two options.

  1. Convert from URL – You can generate a web page that displays the HTML as you desire and then use the Convert Webpage to PDF. Api2Pdf will visit that URL and convert it to PDF accordingly. Another approach is to save the HTML to a .html file type and upload that to a file server like Amazon S3. Then pass the URL to the .html file the same way as you would any URL to generate the PDF.
  2. Convert HTML to PDF straight away by using the POST endpoint to send the raw HTML directly.

Important Point

Manipulate the width. wkhtmltopdf and Headless Chrome render PDFs sort of in the Responsive state, so assume a small width browser. You may have to continuously adjust the widths of the charts via css and the highcharts settings to ensure it looks right.

Tags: , , , ,

Comments are closed.