当前位置: 首页 > news >正文

三网合一网站系统晋城市网站建设

三网合一网站系统,晋城市网站建设,wordpress的ftp,久商推网站建设安装libreoffice见&#xff1a;Centos安装unoconv文档转换工具并在PHP中使用phpword替换word模板中的变量后&#xff0c;使用unoconv将word转换成pdf-CSDN博客 <?php namespace app\lib; use think\Exception; /** * 单个或批量将Word文件转换为PDF * 需要服务器安装L…

安装libreoffice见:Centos安装unoconv文档转换工具并在PHP中使用phpword替换word模板中的变量后,使用unoconv将word转换成pdf-CSDN博客

<?php
namespace app\lib;

use think\Exception;

/**
 * 单个或批量将Word文件转换为PDF
 * 需要服务器安装LibreOffice
 * yum install -y libreoffice.x86_64
 */
class WordToPdfConverter {
    // LibreOffice可执行文件路径
    private $libreOfficePath;
    
    // 构造函数,设置LibreOffice路径
    public function __construct($libreOfficePath = '/usr/bin/libreoffice') {
        $this->libreOfficePath = $libreOfficePath;
    }
    
    /**
     * 检查LibreOffice是否可用
     */
    public function checkLibreOffice() {
        if (!file_exists($this->libreOfficePath)) {
            throw new Exception("LibreOffice未找到,请检查路径设置");
        }
        return true;
    }

    /**
     * 转换单个Word文件为PDF
     * @param string $inputFile 输入Word文件路径
     * @param string $outputDir 输出PDF目录
     * @return bool 转换是否成功
     */
    public function convertToPdfByShellExec($inputFile, $outputDir) {
        // 检查输入文件是否存在
        if (!file_exists($inputFile)) {
            throw new Exception("输入文件不存在: " . $inputFile);
        }
        
        // 确保输出目录存在
        if (!file_exists($outputDir)) {
            mkdir($outputDir, 0755, true);
        }
        
        // 获取文件名(不含扩展名)
        $filename = pathinfo($inputFile, PATHINFO_FILENAME);
        
        // 核心-构建转换命令
        // --headless: 无界面模式
        // --convert-to pdf: 转换为PDF
        // --outdir: 输出目录
        //libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir /output/path input.docx
        $command = escapeshellcmd($this->libreOfficePath) .
        ' --headless --convert-to pdf:writer_pdf_Export --outdir '
            .escapeshellarg($outputDir).' '.escapeshellarg($inputFile).' 2>&1 &';
            
            // 执行命令
            shell_exec($command);
            // 检查是否转换成功
            $pdfFile = $outputDir . '/' . $filename . '.pdf';
            if (file_exists($pdfFile)) {
                return [
                    'success' => true,
                    'pdf_path' => $pdfFile,
                    'message' => '转换成功'
                ];
            } else {
                return [
                    'success' => false,
                    'input_file' => $inputFile,
                    'message' => '转换失败'
                ];
            }
    }
    
    /**
     * 转换单个Word文件为PDF
     * @param string $inputFile 输入Word文件路径
     * @param string $outputDir 输出PDF目录
     * @return bool 转换是否成功
     */
    public function convertToPdfByExec($inputFile, $outputDir) {
        // 检查输入文件是否存在
        if (!file_exists($inputFile)) {
            throw new Exception("输入文件不存在: " . $inputFile);
        }
        
        // 确保输出目录存在
        if (!file_exists($outputDir)) {
            mkdir($outputDir, 0755, true);
        }
        
        // 获取文件名(不含扩展名)
        $filename = pathinfo($inputFile, PATHINFO_FILENAME);
        
        // 核心-构建转换命令
        // --headless: 无界面模式
        // --convert-to pdf: 转换为PDF
        // --outdir: 输出目录
        //libreoffice --headless --convert-to pdf:writer_pdf_Export --outdir /output/path input.docx
        $command = escapeshellcmd($this->libreOfficePath) .
        ' --headless --convert-to pdf:writer_pdf_Export --outdir '
            .escapeshellarg($outputDir).' '.escapeshellarg($inputFile).' 2>&1 &';
            
        // 执行命令
        \exec($command, $output, $returnVar);
        
        // 检查是否转换成功
        $pdfFile = $outputDir . '/' . $filename . '.pdf';
        if ($returnVar === 0 && file_exists($pdfFile)) {
            return [
                'success' => true,
                'pdf_path' => $pdfFile,
                'message' => '转换成功'
            ];
        } else {
            return [
                'success' => false,
                'input_file' => $inputFile,
                'message' => '转换失败,错误码: ' . $returnVar . ', 输出: ' . implode("\n", $output)
            ];
        }
    }

    /**
     * 批量转换目录中的Word文件
     * @param string $inputDir 输入目录
     * @param string $outputDir 输出目录
     * @param array $extensions 要处理的文件扩展名
     * @return array 转换结果
     */
    public function batchConvertToPdf($inputDir, $outputDir, $extensions = ['doc', 'docx']) {
        if (!is_dir($inputDir)) {
            throw new Exception("输入目录不存在: " . $inputDir);
        }
        
        $results = [];
        $directory = new \RecursiveDirectoryIterator($inputDir);
        $iterator = new \RecursiveIteratorIterator($directory);
        $regex = new \RegexIterator($iterator, '/^.+\.(' . implode('|', $extensions) . ')$/i', \RecursiveRegexIterator::GET_MATCH);
        
        foreach ($regex as $file) {
            $filePath = $file[0];
            $results[] = $this->convertToPdfByShellExec($filePath, $outputDir);
        }
        
        return $results;
    }
}

/*******************************************************

<?php
namespace app\index\controller;

use app\BaseController;
use PhpOffice\PhpWord\TemplateProcessor;
use app\lib\WordToPdfConverter;
use think\Exception;
class Ceshi extends BaseController
{
    public function index(){
        //使用示例
        try {
            $documentTemplate = './222.docx';
            $doc = new TemplateProcessor($documentTemplate);
            $replace = '============*****==================';
            $replace01 = '*******申请人张三有限责任公司********';
            $doc->setValue('val001', $replace);
            $doc->setValue('val002', $replace01);
            //Temporary document filename (with path)
            $tempDocumentFilename = $doc->save();
            
            // 根据操作系统设置正确的LibreOffice路径
            // Windows示例: 'C:/Program Files/LibreOffice/program/soffice.exe'
            // Linux示例: '/usr/bin/libreoffice'
            // Mac示例: '/Applications/LibreOffice.app/Contents/MacOS/soffice'
            $converter = new WordToPdfConverter('/usr/bin/libreoffice');
            
            // 检查LibreOffice是否可用
            $converter->checkLibreOffice();
            
            // 设置输入文件和输出目录
            $inputFile = $tempDocumentFilename;
            // PDF输出目录
            $outputDir = '.';
            
            $res_arr = $converter->convertToPdfByShellExec($inputFile, $outputDir);
            if($res_arr['success']){
                dd(rename($res_arr['pdf_path'], './123.pdf'));
            }else{
                dd($res_arr);
            }
        } catch (Exception $e) {
            echo "错误: " . $e->getMessage() . "\n";
            exit;
        }
    }
}

http://www.dtcms.com/a/577774.html

相关文章:

  • 智慧幼儿园管理系统-幼儿园多园区管理小程序的技术架构与应用实践:重构幼教领域数字化管理范式-幼儿园小程序开发-幼儿园软件开发-幼儿园系统开发定制
  • 精准招聘新纪元:AI 重构选才逻辑
  • 超聚变联手英特尔打造边缘智算一体机,重构工作站市场格局
  • 英国服务器Windows系统远程桌面安装与优化
  • 青岛做网站优化大屏网站模板
  • 多项分布 (Multinomial Distribution)
  • 网站gif横幅广告怎么做网站开发人员篡改客户数据
  • 大模型-vllm的知识点记录-1
  • 哪些网站是用织梦做的php做的直播网站
  • 为云原生加速:深入解析PoleFS分布式缓存系统BlobCache
  • xml方式实现AOP
  • XML签名
  • 云原生基石的试金石:基于 openEuler 部署 Docker 与 Nginx 的全景实录
  • 浏阳网站建设卷云网络南和网站seo
  • postgresql pg_upgrade源码阅读--doing
  • oracle导出 导入
  • 如何自己做个简单网站wordpress 中国提速
  • 程序安装包在ubuntu安装教程,以opencv安装为例
  • Linux 服务器内存监控与优化指南
  • APP应用怎么选择游戏盾
  • 医院网站建设联系方式为企业做一件小事
  • 天津建设网投标网站群晖 wordpress 怎么映射到外网
  • HCIP AI 认证含金量高吗?报考条件是什么?
  • WebActiveX浅析
  • 普中51单片机学习笔记-点亮第一个LED
  • 光子精密QM系列闪测仪——为精准而生,为智造而来
  • docker 下搭建 nacos
  • 【stm32简单外设篇】- EC11 旋转编码器(带按键)
  • R语言机器学习算法实战系列(三十)多组ROC曲线分析教程
  • Linux使用tomcat发布vue打包的dist或html