Home php教程 php手册 Linux 下 php 转DOC转PDF转SWF实现百度的文库预览功能

Linux 下 php 转DOC转PDF转SWF实现百度的文库预览功能

Jun 21, 2016 am 08:49 AM
path quot

去年开发了一个OA系统,需要实现文档一键上传并实现在线预览,类似百度文库的功能。

系统环境:CentOs5.5
用到的工具:Openoffice 3 , Pdf2Swf tool , Jodconverter , FlexPaper

网上找了些资料,早有人已经实现了这样的功能,只不过是用JAVA来写的东东,PHP的没找着。
结合网上的资料根据实现操作经验。
纪录并总结一下:

整体思路如下:
实现步骤: 1. DOC上传   2. DOC转成PDF   3. PDF转成SWF  4. 显示
基于对Linux环境引入openoffice sdk + pdf2swf tool,分两个步骤,先利用openoffice sdk把文档统一转成pdf,然后利用pdf2swf tool把pdf转成swf

实现过程
要实现DOC转成PDF,在LINUX下面有Openoffice可以实现。
那么首先需要的是安装Openoffice这个东东。
1. 安装openoffice3,这个安装过程很纠结,遇到过各种问题,因为先后在几台服务器上安装过,最顺利的安装方法如下,如果遇到问题请看前面的文章有讲openoffice的安装。

tar zxvf OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_zh-CN.tar.gz
cd RPEM
rpm -ivh *.rpm --nodeps –force
安装后的默认目录是在:/opt/目录下面
启动服务:
/opt/openoffice.org3/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
在CentOs下面会缺少黑体和宋体的,直接找到Windows下面的字体目录把黑体和宋体复制进去到目录

/opt/openoffice.org/basis3.3/share/fonts/truetype/
需要重启后才会生效
2. 安装OpenOffice SDK3.3

tar zxvf OOo-SDK_3.3.0_Linux_x86-64_install-rpm_en-US.tar.gz
cd OOO330_m20_native_packed-1_en-US.9567/RPMS/
rpm -vih *.rpm
3. 安装jodconverter.2.2.2 ,安装了这个之后就已经可以实现DOC转PDF了。
这个安装很简单,直接上网站下一个这个东东回来。
解压,复制到一个目录里面去,就能直接用了,调用它里面的/lib/jodconverter-cli-2.2.2.jar这个玩意儿就行,可以直接运行命令测试:     

java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar /tmp/1.doc /tmp/1.pdf
4. 安装swftools
中文支持安装:

mkdir –p /usr/share/xpdf
cd /usr/share/xpdf/
下载中文支持及字体库
wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
wget http://www.nginxs.com/download/font.zip
tar zxvf xpdf-chinese-simplified.tar.gz
unzip font.zip
mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/ cd /usr/share/xpdf/xpdf-chinese-simplified
编辑,加入新增的字体

vim add-to-xpdfrc
内容如下:
cidToUnicodeAdobe-GB1/usr/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMapISO-2022-CN/usr/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMapEUC-CN/usr/share/xpdf/chinese-simplified/EUC-CN.unicodeMap
unicodeMapGBK/usr/share/xpdf/chinese-simplified/GBK.unicodeMap
cMapDirAdobe-GB1/usr/share/xpdf/chinese-simplified/CMap toUnicodeDir/usr/share/xpdf/chinese-simplified/CMap
displayCIDFontTT Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap/gkai00mp.ttf
保存后退出
SwfTool安装:
cd /usr/local/wenku
wget http://www.swftools.org/swftools-0.9.1.tar.gz
tar zxvf swftools-0.9.1.tar.gz
cd swftools-0.9.1
./configure 6)make
make install
测试一下是否可用
pdf2swf -o /usr/output.swf -T -z -t -f /usr/test1.pdf -s languagedir=/usr/local/share/xpdf/chinese-simplified -s flashversion=9
如果测试成功,那么就已经OK一大半了,就是显示的问题了。

5. 用FlexPaper实现在线预览
这个是网上下载吧,看着demo改下JS就行了,里面是用的JS读取SWF的路径,这个简单吧,不讲了。


PHP来调用LINUX命令实现转换,可能会遇到一个问题说PHP没有执行权限,注意把PHP的用户改成权限也就是nobody这个用户的权限。


下面是我自己写的代码,PHP初学不久,不够精简,仅供参考
Yii框架中文件转换功能,DOC转SWF

/**
文件转换
oscar 2011-11-25 jincan.liu@gmail.com
*/
class converter extends CWidget{

public function run($file)
{

$filesArr = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt');
//文件上传并转换
if($_FILES["file"] && $_FILES["file"]["error"]
$fileName = iconv('UTF-8','gb2312',$_FILES["file"]["name"]);
$types = explode('.',$fileName);
$typesIf = $types[1];
//改名为时间戳
$types[0] = time();
$fileName = $types[0].'.'.$types[1];
$filetype = $typesIf;

//限制上载类型
if(!in_array($typesIf,$filesArr)){
echo '';
}
/*
function check_is_chinese($s){
return preg_match('/[\x80-\xff]./', $s);
}
//检测中文文件名
if (check_is_chinese($fileName)) {
$types[0] = time();
$fileName = $types[0].'.'.$types[1];
}
*/

//更改路径
if($typesIf=='pdf'){
$path = 'converter/files/pdf/';
}else{
$path = 'converter/files/doc/';
}

if (file_exists($path . $fileName)){
echo '';
}else{
move_uploaded_file($_FILES["file"]["tmp_name"], $path.$fileName);
//var path
$docpath = '/data/oa/frontend/www/converter/files/doc/';
$pdfpath = '/data/oa/frontend/www/converter/files/pdf/';
$swfpath = '/data/oa/frontend/www/converter/files/swf/';

if (file_exists($path . $fileName)){
//执行转换
if($typesIf=='pdf'){ //PDF 转SWF
$pdf = $fileName;
$swf = str_replace('pdf','swf',$pdf);
exec('/usr/local/wenku/swftools/bin/pdf2swf -o '.$swfpath.$swf.' -T -z -t -f '.$pdfpath.$pdf.' -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9');
$path2 = $pdfpath.$pdf;
$path3 = $swfpath.$swf;
}else{ //DOC 转 PDF
$doc = $fileName;
$format = explode('.',$fileName);
$formatName = $format[0].'.pdf';
$command = 'java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar '.$docpath.$doc.' '.$pdfpath.$formatName;
exec($command);
$path1 = $docpath.$doc;
$path2 = $pdfpath.$formatName;

if(file_exists( $pdfpath.$formatName)){
$pdf = $formatName;
$swf = str_replace('pdf','swf',$pdf);
$swfcommand = '/usr/local/wenku/swftools/bin/pdf2swf -o '.$swfpath.$swf.' -T -z -t -f '.$pdfpath.$pdf.' -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9';
exec($swfcommand);
$path3 = $swfpath.$swf;
}
}
}
}

// print_r($_FILES["file"]);
$filetype = isset($filetype) ? $filetype : '';
$path1 = isset($path1) ? str_replace('/data/oa/frontend/www','',$path1) : '';
$path2 = isset($path2) ? str_replace('/data/oa/frontend/www','',$path2) : '';
$path3 = isset($path3) ? str_replace('/data/oa/frontend/www','',$path3) : '';
$reArr = array('filetype' => $filetype,'path1'=>$path1,'path2'=>$path2,'path3'=>$path3);
return $reArr;
}


// php100.com php100中文网

}

//删除文件
public function DelFile($path,$pdfpath,$swfpath){
$pathcommand = 'rm -rf /data/oa/frontend/www'.$path;
exec($pathcommand);
$pdfpathcommand = 'rm -rf /data/oa/frontend/www'.$pdfpath;
exec($pdfpathcommand);
$swfpathcommand = 'rm -rf /data/oa/frontend/www'.$swfpath;
exec($swfpathcommand);
}

}



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

How to set the path environment variable How to set the path environment variable Sep 04, 2023 am 11:53 AM

Method to set the path environment variable: 1. Windows system, open "System Properties", click the "Properties" option, click "Advanced System Settings", in the "System Properties" window, select the "Advanced" tab, and then click "Environment Variables" " button, find and click "Path" to edit and save; 2. For Linux systems, open the terminal, open your bash configuration file, add "export PATH=$PATH: file path" at the end of the file and save it; 3. For MacOS system, the operation is the same as above.

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to correctly set the PATH environment variable in Linux How to correctly set the PATH environment variable in Linux Feb 22, 2024 pm 08:57 PM

How to correctly set the PATH environment variable in Linux In the Linux operating system, environment variables are one of the important mechanisms used to store system-level configuration information. Among them, the PATH environment variable is used to specify the directories in which the system searches for executable files. Correctly setting the PATH environment variable is a key step to ensure the normal operation of the system. This article will introduce how to correctly set the PATH environment variable in Linux and provide specific code examples. 1. Check the current PATH environment variable and enter the following command in the terminal

How to configure path environment variable in java How to configure path environment variable in java Nov 15, 2023 pm 01:20 PM

Configuration steps: 1. Find the Java installation directory; 2. Find the system environment variable settings; 3. In the environment variable window, find the variable named "Path" and click the edit button; 4. In the pop-up edit environment variable window , click the "New" button, and enter the Java installation path in the pop-up dialog box; 5. After confirming that the input is correct, click the "OK" button.

The role and importance of the PATH environment variable in Linux The role and importance of the PATH environment variable in Linux Feb 21, 2024 pm 02:09 PM

"The Role and Importance of the PATH Environment Variable in Linux" The PATH environment variable is one of the very important environment variables in the Linux system. It defines which directories the system searches for executable programs. In the Linux system, when the user enters a command in the terminal, the system will search one by one in the directories listed in the PATH environment variable to see if the executable file of the command exists. If found, it will be executed. Otherwise, "commandnotfound" will be prompted. The role of the PATH environment variable: Simplified

How to configure path in java environment variables How to configure path in java environment variables Apr 22, 2023 pm 06:49 PM

1. Find the bin directory under the jdk installation directory and copy it. 2. Click Computer and select Properties; 3. Select Advanced, Environment Variables; 4. Paste at the path line. Note the English half-width symbol (;) at the end. Administrator user variables are only for Used by the administrator user, system variables can be used by all users. Among environment variables, path is used to ensure that java commands are executed under the path. It can be said to be an indispensable link in environment variable configuration.

How to use file separator and path separator in Java? How to use file separator and path separator in Java? Apr 21, 2023 pm 02:40 PM

1. File delimiters File delimiters are characters used to separate directory names that constitute a path to a specific location. 1. Get the file delimiter In Java, there are several ways to get the file delimiter. We can use File.separator to get the separator of String. StringfileSeparator=File.separator; We can also use File.separatorChar to obtain the character of this separator. charfileSeparatorChar=File.separatorChar; Starting from Java7, we can also use the file system. StringfileSeparator=F

See all articles