html转为图片格式

use Dompdf\Dompdf;


public function getTable($data,$title8,$user_id,$date){

        $dir =  ROOT_PATH."public/".$date;
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        $im =@imagecreate(400, 300) or die("Cannot Initialize new GD image stream");

        imagecolorallocate($im, 0, 0, 0);

        $text_color = imagecolorallocate($im, 233, 14, 91);

        imagestring($im,5,5,5,"Table Image",$text_color);

        imagettftext($im, 10,0, 100, 20,$text_color,ROOT_PATH . 'public/assets/fonts/simsun.ttf','This is a table image.');

        $html = getSelectionTable($data,$title8);

        $dompdf = new Dompdf();

        $dompdf->loadHtml($html, 'UTF-8');

        $dompdf->setPaper('A4', 'landscape');

        $dompdf->render();

        $image =$dompdf->output();

        $pdf_path = ROOT_PATH . "public/page-".$user_id.'-'.time().".pdf";

        file_put_contents($pdf_path, $image);

        $im = new \Imagick();

        $im->readImage($pdf_path);

        $firstPageOnly = true;

        foreach ($im as $k => $frame) {
 
            if ($firstPageOnly && $k == 1) {
                break;
            }

            $frame->setImageFormat('jpg');

            $returnPath =  "/price/".$date."/".sprintf('page-'.time().'.jpg', $k);

            $selectionTablePath = ROOT_PATH . "public".$returnPath;

            file_put_contents($selectionTablePath, $frame->getImageBlob());
        }

        $im->clear();

        $im->destroy();

        unlink($pdf_path);

        return $returnPath;

    }


public function getTable($data,$title8) {
        $htmlContent = '<table>';

        $count = count($data);

        $count_1 = $count + 1;

        $htmlContent .= '<tr><th colspan="9" style="text-align:center;vertical-align: middle;">大金空调<span style="color: #ff0000;align-content: center" >表名</span>A1</th></tr>';

        $htmlContent .= '<tr><th rowspan="'.$count_1.'"></th><th>title1</th><th>title2</th><th>title3</th><th>title4</th><th>title5(W)</th><th>title6</th><th>title7</th><th width="10%">title8</th></tr>';
        foreach ($data as $key=>$row) {
            $htmlContent .= '<tr>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title1']) . '</td>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title2']) . '</td>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title3']) . '</td>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title4']) . '</td>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title5']) . '</td>';
            $htmlContent .= '<td>' . htmlspecialchars($row['title6']) . '</td>';
            if($key == 0){
//                $outdoor_models = str_replace(',',"<br/>",htmlspecialchars($row['title1']));
                $htmlContent .= '<td  rowspan="'.$count.'">' . $title8. '</td>';
                $htmlContent .= '<td  rowspan="'.$count.'">' . htmlspecialchars($row['title9']) . '</td>';
            }
            $htmlContent .= '</tr>';
        }
        $htmlContent .= '</table>';
        $html ='<html>
                 <style>
                  table {
                    width: 100%;
                    border-collapse: collapse;
                    text-align:center;
                    vertical-align: middle;
                  }
                  table, th, td {
                    border: 1px solid black;
                  }
                  th, td {
                    padding: 10px;
                    text-align: left;
                  }
                </style>   
             <body style="font-family:simsun">'.$htmlContent .'</body>
            </html>';
        return $html;
    }

需要安装  imagick扩展 和 imagemagick程序

参考:https://www.cnblogs.com/jinxiblog/p/8053008.html

需要ghostscript

参考:https://blog.csdn.net/qq_22769789/article/details/114382671

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Pyecharts绘制双Y轴图,可以使用Line类和Overlap类。下面是一个简单的例子: ```python from pyecharts import options as opts from pyecharts.charts import Line, Bar, Grid, Overlap from pyecharts.commons.utils import JsCode # 构造数据 x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] y1_data = [820, 932, 901, 934, 1290, 1330, 1320] y2_data = [200, 182, 191, 234, 290, 330, 310] # 创建第一条线 line = ( Line() .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="Line 1", y_axis=y1_data, label_opts=opts.LabelOpts( is_show=False ), linestyle_opts=opts.LineStyleOpts( width=2, type_='dashed' ), itemstyle_opts=opts.ItemStyleOpts( color='blue' ) ) ) # 创建第二条线 bar = ( Bar() .add_xaxis(xaxis_data=x_data) .add_yaxis( series_name="Bar 1", y_axis=y2_data, label_opts=opts.LabelOpts( is_show=False ), itemstyle_opts=opts.ItemStyleOpts( color='red' ) ) ) # 将两个图形叠加在一起 overlap = Overlap() overlap.add(line) overlap.add(bar, yaxis_index=1) # 设置双Y轴 overlap.set_global_opts( yaxis_opts=[ opts.AxisOpts( type_="value", name="Line Y", axislabel_opts=opts.LabelOpts(formatter="{value}"), ), opts.AxisOpts( type_="value", name="Bar Y", position="right", axislabel_opts=opts.LabelOpts(formatter="{value}"), ), ] ) # 将图形转换为png格式图片 overlap.render("chart.html") from PIL import Image from io import BytesIO img = Image.open(BytesIO(overlap.render().encode('utf-8'))) img.save('chart.png') ``` 在这个例子中,我们首先创建了两个数据系列,然后使用Line和Bar类分别创建两个图形。最后,我们使用Overlap类将这两个图形叠加在一起,并使用set_global_opts方法设置双Y轴。最后,我们将图形保存为HTML格式的文件,并使用Pillow库将它转换为PNG格式图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值