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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值