Using the package maatwebsite/excel for Laravel I'm trying to export to PDF an XLSX that has a chart in it.
I can see the chart if I export as an xlsx file, but when exporting to PDF is not showing the chart.
I have tried with all the PDF Renderers:
- dompdf/dompdf
- mpdf/mpdf
- tecnickcom/tcpdf
Without any success.
Using AfterSheet I have manage to render to an external file the chart. But I can't manage to have the chart outputted to the PDF.
// SecondSheet
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$worksheet = $event->sheet->getDelegate();
$chartNames = $worksheet->getChartNames();
foreach ($chartNames as $chartName) {
$chart = $worksheet->getChartByName($chartName);
$chart->render(storage_path('app/' . $chartName . '.png'));
}
}
];
}
I'm exporting the file as:
Excel::store(new TheReportExport($myCollection), $fileName, null, MaatwebsiteExcel::MPDF);
And TheReportExport
file looks like:
<?php
namespace App\Exports;
use App\Exports\Sheets\FirstSheet;
use App\Exports\Sheets\SecondSheet;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Events\BeforeWriting;
class TheReportExport implements WithMultipleSheets, WithEvents
{
private $data;
public function __construct(Collection $data)
{
$this->data = $data;
}
public function sheets(): array
{
$sheets = [];
$sheets[] = new FirstSheet($this->data);
$sheets[] = new SecondSheet($this->data);
return $sheets;
}
public function registerEvents(): array
{
return [];
}
}