Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akilrajdho's avatar

How to print consoletvs Charts in barryvdh PDF

Hey, I am trying to print charts in PDF. I am using this code to generate charts.

$chart = Charts::multi('bar', 'morris') ->title("Total/Active") ->dimensions(0, 265) ->template("c3") ->colors(['#984178', '#03B6BC']) ->dataset('Total', [$users, $projects]) ->dataset('Active', [$activeUsers, $activeProjects]) ->labels(['Users', 'Projects']);

But can not understand how to print those in PDF, some ways I tried was showing error because they couldnt run $chart as string.

But in here is not just 1 chart, but 2, so I was thinking it would be a better idea if I print the entire dashboard.blade.php . it contains only the dashboards

Any suggestion?

0 likes
2 replies
phplavatec's avatar

Am having the same problem, however am using highcharts. Anyone who can help?

artcore's avatar

Use their api to create an image via cUrl for instance https://export.highcharts.com/

here's a snippet of my chart class, not using any 3rd party packages. I'm just creating a json object that I can use to create a chart on their api and store the image on the server to be linked in the pdf

public function renderForExport($options)
  {
    $output = [
      'chart'       => [
        'type' => $options->global->type,
      ],
      'title'       => null,//property_exists($options->global, 'title') ? ['text' => $options->global->title] : ['text' => null],
      'subtitle'    => null,//property_exists($options->global, 'subtitle') ? ['text' => $options->global->subtitle] : ['text' => null],
      'credits'     => ['enabled' => false],
      'xAxis'       => $options->axis['xAxis'] ?? [],
      'yAxis'       => $options->axis['yAxis'] ?? [],
      'legend'      => property_exists($options->global, 'legend') ? $options->global->legend : new \stdClass(),
      'plotOptions' => property_exists($options->global, 'plotOptions') ? $options->global->plotOptions : new \stdClass(),
      'series'      => $options->series,
    ];

    return json_encode($output);
  }

  public function export($image, $options)
  {
    $filesystem = new Filesystem();
    $file       = $image . '.png';

    if (!$filesystem->exists($file)) {

      $options = $this->renderForExport($options->original);

      $url = 'https://export.highcharts.com/';
      $ch  = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POSTFIELDS, 'type=image/png&width=900&options=' . $options);
      curl_setopt($ch, CURLOPT_POST, 1);
      $response = curl_exec($ch);
      curl_close($ch);

      $filesystem->put($file, $response);
    
  }

Please or to participate in this conversation.