这里用到一个插件TCPDF和一个扩展IMAGICK
代码如下:
function htmlTopng($html,$pngpath){
//Loader::import('TCPDF.tcpdf'); //如果是放到extend目录下需要开启并且引用think\Loader;
$pdf = new \TCPDF('A4-L');
$pdf->setfont('stsongstdlight','', 10);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdfpath = ROOT_PATH . 'public' . DS . 'contract/temp.pdf';
$pdf->Output($pdfpath, 'f');
if(!extension_loaded('imagick')){
return false;
}
if(!file_exists($pdfpath)){
return false;
}
$imagick = new \IMAGICK(); //宝塔上可以直接开启此扩展
$imagick->setResolution(120,120);
$imagick->setCompressionQuality(100);
$imagick->readImage($pdfpath);
foreach($imagick as $Key => $Var){
$Var->setImageFormat('png');
$Filename = $pngpath.'/'.md5($Key.time()).'.png';
if($Var->writeImage($Filename)==true){
$Return[]= $Filename;
}
}
return $Return;
}