Skip to content

How can I batch export charts to jpg/png files in PHP on server (without web browser)?

YES! You can export the image to either the client-side or on the server-side. You can even run batches to generate charts from the server to any folder with write permission. 

For complete clientless/headless (no web browser), it is recommended to use "wkhtml", an open source command line tools that renders HTML into PDF and various image formats using the Qt WebKit rendering engine from https://wkhtmltopdf.org/

Use "wkhtmltoimage" command on the server-side.

Basically, you create the chart as usually how you want them to display in the browser, one chart per file. Once done, you can simply use wkhtmltoimage to generate .png files. The charts are never displayed on the client-side as the images are straight saved on the server-side.

<?php
$dir = new DirectoryIterator(dirname(__FILE__));

foreach ($dir as $fileinfo) {

    if (!$fileinfo->isDot()) {

        $fileName = $fileinfo->getFilename();
       
        //echo $fileName ."\n";
        $shellout = shell_exec("php $fileName | wkhtmltoimage - ../tmp/$fileName.png" );

    }
}

Feedback and Knowledge Base