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