0

My code is like this:

<script type="text/javascript">
    google.charts.load('current', {
    'packages': ['corechart']
    });

    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
        var data = google.visualization.arrayToDataTable([
        ['Questions', 'Result'],
        ['Correct Answers', <?= !empty($student_result[0] 
        ['correct_ans_count']) ? $student_result[0]['correct_ans_count'] : '0'; ?>],
        ['Wrong Answers', <?= !empty($student_result[0]['wrong_ans_count']) ? $student_result[0]['wrong_ans_count'] : '0'; ?>],
        ['Skipped Questions', <?= !empty($student_result[0]['skiped_que_count']) ? $student_result[0]['skiped_que_count'] : '0'; ?>],
     ]);

        var options = {
        title: 'Test Result Piechart',
        pieHole: 0.4,
        chartArea: {
        left: 100,
        top: 70,
        width: '100%',
        height: '80%'
        }
    };
    var chart_area = document.getElementById('piechart');
    var chart = new google.visualization.PieChart(chart_area);

    google.visualization.events.addListener(chart, 'ready', function() 
    {
        chart_area.innerHTML = '<img src="' + chart.getImageURI() + '" class="img-responsive">';
     });
        chart.draw(data, options);
    }
</script>

My html code is:

<div id="piechart" style="width: 100%; max-width:900px; height: 500px; ">
</div>

when I export to pdf using mpdf using codeigniter 3.x then it wont show graph in pdf.

My php(codeigniter) code is:

$data=array('name'=>'abc');
$html = $this->load->view('view_result',$data,true);
    $this->load->library('M_pdf');
    
    $mpdf = new mPDF();
    
    $mpdf->AddPage(
            'P', // L - landscape, P - portrait
            '', '', '', '', 4, // margin_left
            4, // margin right
            4, // margin top
            4, // margin bottom
            4, // margin header
            4); // margin footer
    //            die;
    ob_clean();
    $mpdf->allow_charset_conversion = true;
    
    $mpdf->SetDisplayMode('fullpage');

    $mpdf->WriteHTML($html);

    $mpdf->Output('test.php', 'D');

In view it shows the graph correctly but in pdf there is no graph only blank space is there. In there any solution to show google chart in mpdf. please help thank you.

w.Daya
  • 443
  • 1
  • 7
  • 12
  • If I understand correctly, you are passing JS code to mPDF? – Olivier Aug 11 '21 at 12:19
  • I don't think it'll work with mPDF since it only supports basic Javascript using `SetJS()` function of mpdf library. [jsPDF](https://github.com/MrRio/jsPDF) is probably what you need. But if you still want to use mPDF, using `chart.getimageuri()` will do the trick. Does this answer your question? https://stackoverflow.com/questions/46417248/how-to-export-google-chart-in-pdf. – NcXNaV Aug 11 '21 at 12:31
  • Yes @Olivier JS code is written in script tag in view page. – w.Daya Aug 12 '21 at 07:40
  • Then it can't work because obviously mPDF doesn't have a JS interpreter (mPDF is just a PHP script, not a browser). – Olivier Aug 12 '21 at 07:42
  • Hay I Solved it. – w.Daya Aug 13 '21 at 05:19
  • Still you can post your solution if any – w.Daya Aug 13 '21 at 06:48
  • https://stackoverflow.com/questions/46417248/how-to-export-google-chart-in-pdf , not useful for me @NcXNaV – w.Daya Aug 13 '21 at 06:50
  • We just have to send that file path to the php script where mpdf (pdf)generation code is written. and assign that value to the image tag in view (passed to create pdf) and hide originally written code to show graph. – w.Daya Aug 18 '21 at 10:58

0 Answers0