编程语言
首页 > 编程语言> > php-如何在Html2Pdf中添加ChartJS代码以查看图像

php-如何在Html2Pdf中添加ChartJS代码以查看图像

作者:互联网

我正在使用Chart.js.

这是我的php文件,我使用过javascript,通过它我可以简单地创建图表并在此之后创建图表图像.

<script>

            function done(){

                    var url=myLine.toBase64Image();
                    document.getElementById("url").src=url;
            }


        var MONTHS = [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>];


        var config = {
          type: 'line',
          data: {

            labels: [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>],

            datasets: [{
              label: "<?php echo $asx_code ?>",
              backgroundColor: '#ff6384', 
              borderColor:'#ff6384', 

              data: [<?php echo '"' . implode('","', array_reverse($close_price_arr)) . '"' ?>],
              fill: false,
            }]
          },
          options: {
               bezierCurve : false,
      animation: {
        onComplete: done
      },
            responsive: true,
            title: {
              display: true,
              text: ''
            },
            tooltips: {
              mode: 'index',
              intersect: false,
            },
            hover: {
              mode: 'nearest',
              intersect: true
            },
            scales: {
              xAxes: [{
                display: true,
                scaleLabel: {
                  display: true,
                  labelString: 'Month'
                }
              }],
              yAxes: [{
                display: true,
                scaleLabel: {
                  display: true,
                  labelString: 'Value'
                }
              }]
            }
          }
        };


          var ctx = document.getElementById('canvascode').getContext('2d');
          window.myLine = new Chart(ctx, config);


        document.getElementById('randomizeData').addEventListener('click', function() {
          config.data.datasets.forEach(function(dataset) {
            dataset.data = dataset.data.map(function() {
              return randomScalingFactor();
            });

          });

          window.myLine.update();
        });

        var colorNames = Object.keys(window.chartColors);

      </script>

这绝对正常,但现在我在html2pdf中仍然做同样的事情.我想包含几个JS文件,并使用脚本标签生成图表,然后将图像放入PDF文件中.

这是我所做的一切,没有任何运气.

<?php 
require __DIR__.'/vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;


$html2pdf = new Html2Pdf();
$html2pdf->pdf->IncludeJS('Chart.bundle.js');
$html2pdf->pdf->IncludeJS('chart_utils.js');




$asx_code = 'CHK';

$url = 'https://www.asx.com.au/asx/1/share/' . $asx_code . '/prices?interval=daily&count=365';
  //  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL, $url);
// Execute
$result = curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
$record_decode = json_decode($result);

$close_date_arr = array();
$close_price_arr = array();


    foreach ($record_decode->data as $current_data) {

        $close_price_arr[] = $current_data->close_price;

        $close_date_arr[] = date('F Y', strtotime($current_data->close_date));


    }

$str = '<canvas id="canvascode" style="display:none"></canvas>
<img id="url" style="width:400px; height:400px" />';

?>

<script>

        function done(){

                var url=myLine.toBase64Image();
                document.getElementById("url").src=url;
        }


    var MONTHS = [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>];


    var config = {
      type: 'line',
      data: {

        labels: [<?php echo '"' . implode('","', array_reverse($close_date_arr)) . '"' ?>],

        datasets: [{
          label: "<?php echo $asx_code ?>",
          backgroundColor: '#ff6384', 
          borderColor:'#ff6384', 

          data: [<?php echo '"' . implode('","', array_reverse($close_price_arr)) . '"' ?>],
          fill: false,
        }]
      },
      options: {
           bezierCurve : false,
  animation: {
    onComplete: done
  },
        responsive: true,
        title: {
          display: true,
          text: ''
        },
        tooltips: {
          mode: 'index',
          intersect: false,
        },
        hover: {
          mode: 'nearest',
          intersect: true
        },
        scales: {
          xAxes: [{
            display: true,
            scaleLabel: {
              display: true,
              labelString: 'Month'
            }
          }],
          yAxes: [{
            display: true,
            scaleLabel: {
              display: true,
              labelString: 'Value'
            }
          }]
        }
      }
    };


      var ctx = document.getElementById('canvascode').getContext('2d');
      window.myLine = new Chart(ctx, config);


    document.getElementById('randomizeData').addEventListener('click', function() {
      config.data.datasets.forEach(function(dataset) {
        dataset.data = dataset.data.map(function() {
          return randomScalingFactor();
        });

      });

      window.myLine.update();
    });

    var colorNames = Object.keys(window.chartColors);

  </script>

 <?php  
$html2pdf->writeHTML($str);
$html2pdf->output();

?>

尝试生成PDF文件时,出现错误消息,

Fatal error: Uncaught Spipu\Html2Pdf\Exception\HtmlParsingException: The html tag [canvas] is not known by Html2Pdf. You can create it and push it on the Html2Pdf GitHub project. in /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php:1435 Stack trace: #0 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1043): Spipu\Html2Pdf\Html2Pdf->_executeAction(Object(Spipu\Html2Pdf\Parsing\Node)) #1 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(749): Spipu\Html2Pdf\Html2Pdf->_setNewPositionForNewLine(NULL) #2 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1415): Spipu\Html2Pdf\Html2Pdf->_setNewPage() #3 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(1401): Spipu\Html2Pdf\Html2Pdf->_executeAction(Object(Spipu\Html2Pdf\Parsing\Node)) #4 /var/www/html/htmlpdf/vendor/spipu/html2pdf/src/Html2Pdf.php(595): Spipu\Html2Pdf\Html2Pdf->_makeHTMLcode() #5 /var/www/html/htmlpdf/test.php(138): Spipu\Html2Pdf\Html2Pdf->writeHTML(‘

有人可以指导我如何在这里生成脚本并将仅图像转换为PDF.

解决方法:

如错误消息所提示:您将在Github上转到Html2Pdf并在那里创建新的问题;为了将标记画布添加到库的下一个版本中-或尝试使用画布以外的其他方法来绘制图表;其他图表库也可以与div或svg标签一起使用.

好吧,请参见issue 372 …作者不打算添加标签画布.

因此,您只能解决例如.使用phantomJS创建画布的屏幕截图,然后将其用作静态图像资源,以便将其呈现为PDF.

标签:chart-js,html2pdf,php
来源: https://codeday.me/bug/20191108/2008487.html