php将word转换为pdf

word转pdf(通过命令进行转)

    1. 服务器上安装LIbreOffice
      使用 yum 安装 LibreOffice:
      sudo yum update -y
      安装 LibreOffice:
      sudo yum install libreoffice-headless libreoffice-writer libreoffice-calc libreoffice-impress -y
      查看安装成功:
      soffice --version
    1. 直接上代码:
php 复制代码
    public function convertDocxToPdf()
    {
        $docxPath = ROOT_PATH."template/template.docx";
        // 确保 LibreOffice 在系统路径中可用
        $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;
    }
相关推荐
ServBay2 天前
告别面条代码,PSL 5.0 重构 PHP 性能与安全天花板
后端·php
JaguarJack4 天前
FrankenPHP 原生支持 Windows 了
后端·php·服务端
BingoGo4 天前
FrankenPHP 原生支持 Windows 了
后端·php
JaguarJack5 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo5 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack6 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay7 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954487 天前
CTF 伪协议
php
BingoGo9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack9 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端