zoukankan      html  css  js  c++  java
  • PHP使用LibreOffice实现word转pdf

    LibreOffice是OpenOffice办公套件衍生版, 同样免费开源,并且是跨平台的,支持windows和linux服务器安装。

    php调用命令实现文档转换:

    $retval = 1;
    // exec() might be disabled
    $cmd = 'D:/soft/LibreOfficePortablePrevious/LibreOfficeWriterPortable.exe  --headless --convert-to pdf 1.doc --outdir ./';
    if (function_exists('exec')){
        @exec($cmd, $output, $retval);
    }
    // Did it work?
    if ($retval > 0){
        exit('process_failed');
    }
    echo 'success';

    Centos下的安装及使用

    直接使用yum安装即可:

    1. yum install libreoffice
    2. yum install libreoffice-headless  //该工具用来启动Libreoffice

    2、执行命令转换pdf文档

    跟windows类似,执行以下命令即可转换文档:

    soffice --headless --convert-to pdf {文档路径} --outdir {导出目录路径}

    3.解决中文不显示,变方框的问题

    在linux下转换pdf,可能会出现中文字体不被识别,变成小方框的问题,解决步骤:
    1.把Windows下的字体C:WindowsFonts下的常用字体,即simsun.ttc等复制到usr/share/fonts目录下。
    2 修改字体权限

    sudo chmod 644 simsun.ttc

    3. 更新字体缓存:

    sudo fc-cache -fv
    【杀死子进程命令】
    ps -ef|grep soffice | awk '{print $2}' | xargs kill -9
     
    【重启命令】
    /usr/lib64/libreoffice/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

     【重启成功截图】

    参考:http://www.884358.com/php-libreoffice-word-pdf/

  • 相关阅读:
    [APIO2012]派遣
    Luogu_2774 方格取数问题
    Luogu4149 [IOI2011]Race
    Luogu_4886 快递员
    Loj_6282. 数列分块入门 6
    LOJ 6281 数列分块入门 5
    三维偏序 cdq
    【逆序对】 模板
    【luogu P1637 三元上升子序列】 题解
    【luogu P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀布】 题解
  • 原文地址:https://www.cnblogs.com/yangzailu/p/14308085.html
Copyright © 2011-2022 走看看