libreoffice将world、excel等文件转化为pdf文件

我是用的linux系统,Centos;

第一步:需要下载libreoffice包,选择你直接想要的版本;

下载地址:https://zh-cn.libreoffice.org/download/libreoffice-still/

第二步:将下载好的文件上传到服务器,自己想上传到那个目录就上传到那个目录,我是在root目录下

命令:rz

此命令需要安装:yum install -y lrzsz

解压:tar  -zxvf  Libreoffice_5.4.7_Linux_x86-64_rpm.tar.gz

进入:cd  Libreoffice_5.4.7.2_linux_x86-64_rpm/RPMS/

里面有许多    xxxx.rmp文件

将他们全部安装 yum install *.rpm

第三步:测试是否安装成功

上传一个1.docx的world文档

使用命令:libreoffice5.4 --invisible  --convert-to pdf 1.docx  //最好加上版本号,不然有可能报找不到该命令

然后使用命令 ll  查看是否生成 1.pdf文件,可以使用sz命令下载到本地查看

文件有可能出现乱码

第四步:解决乱码

 可以直接在本地电脑上面  C:\Windows\Fonts  里面找语言包,也可以直接在网上下载

打包压缩上传到服务器 /usr/share/fonts/,重启服务器或者   sudo fc-cache -fv 刷新即可

PHP代码生成将文件转化成pdf


*linux 利用 libreoffice 将world转化为pdf文件
 * $pdfPathpdf  文件保存的目录
 * $pdfHomePath 项目目录或项目中pdf保存的目录
 * $doc_path word文件路径(可以是world,excel表格,TXT文档)
 *
 * 注意文件名最好不要带有中文
 *
 * */

function worldPdf($pdfPath,$pdfHomePath,$doc_path){
    if(!is_dir($pdfPath)){
        mkdir($pdfPath,0777,true);
    }                                                   //最好加上版本号,不然可能会出错
    $str=exec("export HOME={$pdfHomePath} && libreoffice5.4 --invisible --convert-to pdf {$doc_path}");//执行转化命令
    if(empty($str)){
        return 'false';
    }else{
        return 'true';
    }
}

执行此命令可能会报警告:Warning: exec() has been disabled for security reasons

进入php.ini文件,查找 disable_functions 将等号后面的 exec去掉即可,如果还是不行,重启服务器

 

前端实现pdf预览,使用 jquery.media.js

html代码:


<html>

<head>

<meta charset="utf-8">

<meta name="renderer" content="webkit|ie-comp|ie-stand">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>

<script type="text/javascript" src="./jquery.media.js"></script>

</head>

<body >

 <div id="handout_wrap_inner" style="height:500px;">

 

 </div>

</body>

<script>

$(function(){

$('#handout_wrap_inner').media({

        width: '300px;',

        height: '800px;',

        autoplay: true,

        src:'./04403180021102461312.pdf',

            }); 

})

</script>

</html>

 

jquery.media.js


/*
 * jQuery Media Plugin for converting elements into rich media content.
 *
 * Examples and documentation at: http://malsup.com/jquery/media/
 * Copyright (c) 2007-2010 M. Alsup
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * @author: M. Alsup
 * @version: 0.99 (05-JUN-2013)
 * @requires jQuery v1.1.2 or later
 * $Id: jquery.media.js 2460 2007-07-23 02:53:15Z malsup $
 *
 * Supported Media Players:
 *    - Flash
 *    - Quicktime
 *    - Real Player
 *    - Silverlight
 *    - Windows Media Player
 *    - iframe
 *
 * Supported Media Formats:
 *     Any types supported by the above players, such as:
 *     Video: asf, avi, flv, mov, mpg, mpeg, mp4, qt, smil, swf, wmv, 3g2, 3gp
 *     Audio: aif, aac, au, gsm, mid, midi, mov, mp3, m4a, snd, rm, wav, wma
 *     Other: bmp, html, pdf, psd, qif, qtif, qti, tif, tiff, xaml
 *
 * Thanks to Mark Hicken and Brent Pedersen for helping me debug this on the Mac!
 * Thanks to Dan Rossi for numerous bug reports and code bits!
 * Thanks to Skye Giordano for several great suggestions!
 * Thanks to Richard Connamacher for excellent improvements to the non-IE behavior!
 */
/*global SWFObject alert Sys */
/*jshint forin:false */
;(function($) {
"use strict";    

var mode = document.documentMode || 0;
var msie = /MSIE/.test(navigator.userAgent);
var lameIE = msie && (/MSIE (6|7|8)\.0/.test(navigator.userAgent) || mode < 9);

/**
 * Chainable method for converting elements into rich media.
 *
 * @param options
 * @param callback fn invoked for each matched element before

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值