laravel how to add multiple charts to pdf?

Viewed 28

I have drawn multiple chart js charts in blade like this :

  @foreach($charts as $chart => $value)
                    
                    <div class="max-w-lg">
                        <canvas id="{{$chart}}" height="100px"></canvas>
                        
                        <script type="text/javascript">
                            var labels =  {{ Js::from($labels) }};
                            var datas =  {{ Js::from($value) }};
                    
                            var data = {
                            labels: labels,
                            datasets: [{
                                label: {{Js::from($chart)}} + ' Self',
                                backgroundColor: 'white',
                                borderColor: 'blue',
                                data: datas,
                            }]
                            };
                    
                            var config = {
                                type: 'line',
                                data: data,
                                options: {}
                            };
                    
                            var {{$chart}} = new Chart(
                                document.getElementById({{Js::from($chart)}}),
                                config
                            );
                    
                        </script>
                    </div>
                @endforeach

And it works fine. But now i need to add those charts in pdf. I'm using Laravel Snappy. I've tried using QuickChart, and create the chart config in controller :

$chartsUrl = [];
    $i = 0;
    foreach($charts as $chart => $value) {
        $chartConfig = '{
            "type": "line",
            "data": {
                "labels": ["D", "i", "S", "C"],
                "datasets": [{
                "label": $chart . "Self",
                "data": $value
                }]
            }
        }';

        $chartsUrl[$i] = 'https://quickchart.io/chart?w=500&h=300&c=' . urlencode($chartConfig);
        $i++;
    }
    
    $data = compact('result', 'categories', 'labels', 'charts', 'chartsUrl', 'user');

    $pdf = PDF::loadView('pdf.result', $data);
     
    return $pdf->download('result.pdf');

And call the chart url in blade :

@foreach($chartsUrl as $chartUrl)
    <img src="{{ $chartUrl }}" />
@endforeach

But i got the following error :

RuntimeException The exit status code '1' says something went wrong: stderr: "Loading pages (1/6) [> ] 0% [======> ] 10% [========================> ] 40% [==========================> ] 44% [============================> ] 47% Error: Failed to load https://quickchart.io/chart?w=500&h=300&c={ ++++++++++++++++"type":+"line", ++++++++++++++++"data":+{ ++++++++++++++++++++"labels":+["D",+"i",+"S",+"C"], ++++++++++++++++++++"datasets":+[{ ++++++++++++++++++++"label":+$chart+.+"Self", ++++++++++++++++++++"data":+$value ++++++++++++++++++++}] ++++++++++++++++} ++++++++++++}, with network status code 3 and http status code 0 - Host not found [============================================================] 100% Counting pages (2/6) [============================================================] Object 1 of 1 Resolving links (4/6) [============================================================] Object 1 of 1 Loading headers and footers (5/6) Printing pages (6/6) [> ] Preparing [===================> ] Page 1 of 3 [=======================================> ] Page 2 of 3 [============================================================] Page 3 of 3 Done Exit with code 1 due to network error: HostNotFoundError " stdout: "" command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf" --lowquality "C:\Users\DELLE5~1\AppData\Local\Temp\knp_snappy6322b664b9a3c1.03753315.html" "C:\Users\DELLE5~1\AppData\Local\Temp\knp_snappy6322b664bacda0.74994212.pdf".

0 Answers
Related