Chart.js 图像与 php excel

问题描述 投票:0回答:1

我想做的是使用

toDataURL
将chart.js转换为图像,然后使用ajax将其过去,并使用url为php excel设置图像路径。我想在不使用任何按钮的情况下传递数据,但我的 php excel 没有显示任何内容

脚本

        var canvas = document.getElementById('chart1');
        var imgData = canvas.toDataURL('image/png', 1);

        $.ajax({
            type: "POST",
            url: "?f=excel_customer",
            data: { imgData: imgData },
            success: function(response) {
            console.log(response);
            }
        });

php excel

 if(isset($_POST['imgData'])) {
   
$image= $_POST['imgData'];
         
$objDrawing = new PHPExcel_Worksheet_Drawing();    //create object for Worksheet drawing
$objDrawing->setName('chart');                     //set name to image
$objDrawing->setDescription('chart');              //set description to image
$signature = 'chart1';                             //Path to signature .jpg file
$objDrawing->setPath($image);
$objDrawing->setOffsetX(15);                        
$objDrawing->setOffsetY(15);                        
$objDrawing->setCoordinates('B2');        
$objDrawing->setWidth(90);                 
$objDrawing->setHeight(90);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());  //save image
 }

不知道能不能实现。如果没有,有人可以建议我们用另一种方式做到这一点,但仍然使用 phpexcel

php chart.js phpexcel
1个回答
0
投票

对于您的情况,您可以做您想做的事情

  1. 将图表1数据保存到临时文件中,例如xyz1234.png
  2. 执行语句$objDrawing->setPath($image);其中 $image 是“xyz1234.png”
  3. 保存后(生成 xlsx 文件)
  4. 取消链接(删除此临时文件)

所以 HTML 是

<script
  src="https://code.jquery.com/jquery-3.7.1.js"
  integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4="
  crossorigin="anonymous"></script>


<div id=ajaxreturn></div>

<canvas id="chart1" width="200" height="100" >

<script>
   var c = document.getElementById("chart1");
   var ctx = c.getContext("2d");
   ctx.beginPath();
   ctx.arc(95, 50, 40, 0, 2 * Math.PI);
   ctx.stroke();
</script>



<script>
   var canvas = document.getElementById('chart1');
   var imgData = canvas.toDataURL('image/png', 1);
     //console.log(imgData);

        $.ajax({
            type: "POST",
            url: "testso.php",
            data: { imgData: imgData },
            success: function(response) {
             //  document.getElementById('ajaxreturn').innerHTML=response;
             alert(response); 
            }
        });

</script>

而 php (testso.php) 是

<?php

require_once('./PHPExcel1.8/Classes/PHPExcel.php');
require_once('./PHPExcel1.8/Classes/PHPExcel/IOFactory.php');

function save_base64_image($base64_image_string, $output_file_without_extension, $path_with_end_slash="" ) {
    //usage:  if( substr( $img_src, 0, 5 ) === "data:" ) {  $filename=save_base64_image($base64_image_string, $output_file_without_extentnion, getcwd() . "/application/assets/pins/$user_id/"); }      
    //
    //data is like:    data:image/png;base64,asdfasdfasdf
    $splited = explode(',', substr( $base64_image_string , 5 ) , 2);
    $mime=$splited[0];
    $data=$splited[1];

    $mime_split_without_base64=explode(';', $mime,2);
    $mime_split=explode('/', $mime_split_without_base64[0],2);
    if(count($mime_split)==2)
    {
        $extension=$mime_split[1];
        if($extension=='jpeg')$extension='jpg';
        //if($extension=='javascript')$extension='js';
        //if($extension=='text')$extension='txt';
        $output_file_with_extension=$output_file_without_extension.'.'.$extension;
    }
    file_put_contents( $path_with_end_slash . $output_file_with_extension, base64_decode($data) );
    return $output_file_with_extension;
}


$image = $_POST['imgData'];

$x1=   chr(rand(0,25)+97) ;
$x2=   chr(rand(0,25)+97) ;
$x3=   chr(rand(0,25)+97) ;
$x4=   chr(rand(0,9)+48) ;
$x5=   chr(rand(0,9)+48) ;

$path=$x1.$x2.$x3.$x4.$x5;





save_base64_image($image, $path, $path_with_end_slash="" );


$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("creater");
$objPHPExcel->getProperties()->setLastModifiedBy("test2");
$objPHPExcel->getProperties()->setSubject("testSubject");

//$objWorkSheet = $objPHPExcel->createSheet();
//$objWorkSheet2 = $objPHPExcel->createSheet();


$objPHPExcel->getActiveSheet()->setTitle('test');



$objDrawing = new PHPExcel_Worksheet_Drawing();    //create object for Worksheet drawing
$objDrawing->setName('chart');                     //set name to image
$objDrawing->setDescription('chart');              //set description to image
$signature = 'chart1';                             //Path to signature .jpg file
$objDrawing->setPath($path.'.png');
$objDrawing->setOffsetX(15);                        
$objDrawing->setOffsetY(15);                        
$objDrawing->setCoordinates('B2');        
$objDrawing->setWidth(90);                 
$objDrawing->setHeight(90);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 




// for XLSX
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Content-Disposition: attachment;filename="DOWNLOAD_test'.$ry.'.xlsx"');
//header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


$name = './newexcel.xlsx';
$objWriter->save($name);


unlink($path.'.png'); 

echo "Successful" ;

     ?>

© www.soinside.com 2019 - 2024. All rights reserved.