首先安装第三方类
composer require dompdf/dompdf
composer require phpoffice/phpword
加载字体
参考文章 laravel 使用 dompdf 将html 转为pdf文件
word 转 pdf 核心代码
$filePath = public_path();
$wordName = $filePath . '/test001.docx';//需要转的word文档
$htmlName = $filePath . '/test.html';//html文件地址
$pdfName = $filePath . '/test.pdf';//pdf保存地址
$phpWord = IOFactory::load($wordName);
// 将PhpWord文档转换为HTML
IOFactory::createWriter($phpWord , 'HTML')->save($htmlName);
$html = file_get_contents($htmlName);
@unlink($htmlName);
$options = new Options();
$options->setIsRemoteEnabled(true);
$options->setDefaultFont('simsun');
$dompdf = new Dompdf($options);
$css = '<style>
body {
word-wrap: break-word;
word-break: break-all;
font-size: 10px !important;
}
img{
max-width:100%;
}
</style>';
$html = substr_replace($html, $css, strpos($html, '</head>'), 0);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4');
$dompdf->render();
// file_put_contents($pdfName, $dompdf->output());
$dompdf->stream('document.pdf', ['Attachment' => false]);
exit;