Convert PHP files in Linux, convert Doc to PDF to SwF, and implement Baidu's library preview function

Source: Internet
Author: User

From: http://dungenessbin.diandian.com/post/2012-04-20/19373103

 

 

Last year, an OA system was developed, which requires one-click Document Upload and online preview, similar to the functions of Baidu Library.

System Environment: centos5.5
Tools used: OpenOffice 3, ipv2swf tool, jodconverter, and flexpaper

I have found some information on the Internet. Someone has already implemented this function, but I wrote something in Java, but PHP didn't find it.
Based on the implementation experience of online materials.
Record and summarize:

The overall idea is as follows:
Implementation steps: 1. Upload Doc 2. Convert Doc to PDF 3. Convert PDF to SwF 4. Display
Based on the introduction of OpenOffice SDK + javas2swf tool to the Linux environment, the document is converted into a PDF file by using OpenOffice SDK in two steps, and then converted into a SWF file by using javas2swf tool.

Implementation Process
To convert Doc to PDF, OpenOffice can be implemented in Linux.
First, you need to install OpenOffice.
1. Install openoffice3. This installation process is very tangled and has encountered various problems, because it has been installed on several servers. The most smooth installation method is as follows. If you have any problems, please refer to the previousArticleInstall OpenOffice.

Tar zxvf OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_zh-CN.tar.gz
CD rpem
Rpm-IVH *. rpm -- nodeps-Force
The default directory after installation is:/opt/
Start the service:
/Opt/OpenOffice. org3/Program/soffice-Headless-accept = "socket, host = 127.0.0.1, Port = 8100; URP;"-nofirststartwizard &
In centos, and are missing. You can directly find the font directory under Windows and copy and to the directory.

/Opt/openoffice.org/basis3.3/#/fonts/truetype/
It takes effect only after being restarted.
2. Install 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. Install jodconverter.2.2.2. After installing this, you can convert Doc to PDF.
This installation is very simple. You can directly go to the next website to get this stuff back.
Decompress, copy to a directory, you can directly use it, call it/lib/jodconverter-cli-2.2.2.jar this stuff on the line, you can directly run the command test:

Java-Jar/usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar/tmp/1.doc/tmp/1.pdf
4. Install swftools
Installation supported in Chinese:

Mkdir-P/usr/share/xpdf
CD/usr/share/xpdf/
Download Chinese support and Library
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-Chinese-Simplified
Edit and add the new font

Vim add-to-xpdfrc
The content is as follows:
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
Save and exit
Install 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
Test availability
Export 2swf-O/usr/output.swf-T-z-T-F/usr/test1.pdf-s export agedir =/usr/local/share/xpdf/Chinese-simplified-s flashversion = 9
If the test is successful, more than half of it will be OK, and the problem will be displayed.

5. Online Preview using flexpaper
This is an online download. Just watch the demo and change the JS file. The JS file is used to read the SWF path. This is simple.

PHP calls the Linux Command to implement conversion. It may encounter a problem that PHP has no execution permission. Change the PHP user to the permission, that is, the permission of the user nobody.

Below is my ownCodePHP is short for beginners and is not concise enough for reference only.
File Conversion Function in yii framework, Doc to SwF

<? PHP
/**
File conversion
Oscar 2011-11-25 jincan.liu@gmail.com
*/
Class converter extends cwidget {

Public Function run ($ file)
{

$ Filesarr = array ('pdf ', 'Doc', 'docx', 'xls', 'xlsx', 'ppt ', 'ppt', 'txt ');
// Upload and convert files
If ($ _ FILES ["file"] & $ _ FILES ["file"] ["error"] <= 0 ){

$ Filename = iconv ('utf-8', 'gb2312', $ _ FILES ["file"] ["name"]);
$ Types = explode ('.', $ filename );
$ Typesif = $ types [1];
// Change the name to the timestamp
$ Types [0] = Time ();
$ Filename = $ types [0]. '.'. $ types [1];
$ Filetype = $ typesif;

// Restrict the upload type
If (! In_array ($ typesif, $ filesarr )){
Echo '<SCRIPT type = "text/JavaScript"> alert ("Upload File Types in: PDF, Doc, docx, XSL, XLSX, PPT, pptx, TXT"); location. href = location. href; </SCRIPT> ';
}
/*
Function check_is_chinese ($ s ){
Return preg_match ('/[\ X80-\ xFF]./', $ S );
}
// Check the Chinese file name
If (check_is_chinese ($ filename )){
$ Types [0] = Time ();
$ Filename = $ types [0]. '.'. $ types [1];
}
*/

// Change the path
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) {
// execute the conversion
if ($ typesif = 'pdf ') {// Convert PDF to SwF
$ PDF = $ filename;
$ SWF = str_replace ('pdf ', 'swf', $ PDF );
exec ('/usr/local/wenku/swftools/bin/ipv2swf-O '. $ swfpath. $ SWF. '-T-z-T-F '. $ pdfpath. $ PDF. '-S release agedir =/usr/share/xpdf-Chinese-simplified-s flashversion = 9 ');
$ path2 = $ pdfpath. $ pdf;
$ path3 = $ swfpath. $ SWF;
}else {// Doc to PDF
$ Doc = $ filename;
$ format = explode ('. ', $ filename);
$ formatname = required format=02.16.'hangzhou ';
$ 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/4252swf-O '. $ swfpath. $ SWF. '-T-z-T-F '. $ pdfpath. $ PDF. '-S release agedir =/usr/share/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;
}

}

// Delete an object
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 );
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.