word转pdf(通过命令进行转)
-
- 服务器上安装LIbreOffice
使用 yum 安装 LibreOffice:
sudo yum update -y
安装 LibreOffice:
sudo yum install libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress -y
查看安装成功:
soffice --version
-
- 直接上代码:
public function convertDocxToPdf()
{
$docxPath = ROOT_PATH."template/template.docx";
$pdfFilePath = ROOT_PATH.'pdf/output.pdf';
$command = "soffice --headless --convert-to pdf $docxPath --outdir " . escapeshellarg(dirname($pdfFilePath));
exec($command, $output, $returnVar);
if ($returnVar !== 0) {
throw new \Exception("Error converting to PDF: " . implode("\n", $output));
}
return $pdfFilePath;
}