Linux PHP turn doc to PDF transfer swf to realize Baidu Library preview function

Source: Internet
Author: User
Tags format array explode file upload ftp implement zip linux

Last year, the development of an OA system, need to implement a key document upload and realize online preview, similar to the function of Baidu Library.

System Environment: CentOs5.5
Tools used: Openoffice 3, pdf2swf tool, Jodconverter, Flexpaper

On the Internet to find some information, someone has already realized such a function, but it is written in Java, PHP did not find.
Combined with the data on the Internet to achieve operational experience.
Record and summarize:

The whole idea is as follows:
Implementation steps: 1. Doc uploaded 2. Doc converted to PDF 3. PDF into SWF 4. Show
Based on the introduction of the Linux environment OpenOffice SDK + PDF2SWF tool, two steps, using the OpenOffice SDK to unify the document into a PDF, and then use PDF2SWF tool the PDF into SWF

Implementation process
To implement doc into PDF, there is a OpenOffice under Linux that can be implemented.
Then the first need is to install OpenOffice this dongdong.
1. Installation Openoffice3, this installation process is very tangled, encountered a variety of problems, because successively installed in several servers, the most successful installation methods are as follows, if you encounter problems, please see the previous article has said OpenOffice installation.

Tar zxvf ooo_3.3.0_linux_x86-64_install-rpm-wjre_zh-cn.tar.gz
RPM-IVH *.rpm--nodeps–force
The default directory after installation is under:/opt/directory
To start the service:
/opt/openoffice.org3/program/soffice-headless-accept= "SOCKET,HOST=,PORT=8100;URP;"-nofirststartwizard &
Below the CentOS there will be a lack of bold and Arial, directly to the font directory under Windows copy Bold and Arial into the catalog

Requires a reboot before it will take effect.
2. Installation of 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 can already realize doc turn pdf.
This installation is very simple, directly on the site next to this dongdong back.
Decompression, copied to a directory inside, you can directly use, call it inside the/lib/jodconverter-cli-2.2.2.jar this thing on the line, you can run the command test directly:

4. Install Swftools
Chinese Support installation:

Download Chinese support and font library
wget Http://
Tar zxvf xpdf-chinese-simplified.tar.gz
MV Gbsn00lp.ttf Gkai00mp.ttf xpdf-chinese-simplified/cmap/cd/usr/share/xpdf/xpdf-chinese-simplified
Edit, add the new font

The contents are as follows:
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
Exit after saving
Swftool Installation:
Tar zxvf swftools-0.9.1.tar.gz
CD swftools-0.9.1
./configure 6) Make
Make install
Test to see if it is available
Pdf2swf-o/usr/output.swf-t-z-t-f/usr/test1.pdf-s languagedir=/usr/local/share/xpdf/chinese-simplified-s flashver Sion=9
If the test succeeds, then already OK half, is the problem that displays.

5. Realize online preview with Flexpaper
This is the online download bar, looking at the demo to change JS on the line, which is the use of JS read SWF path, this simple bar, do not speak.

PHP to invoke the Linux command to implement the conversion, you may encounter a problem that PHP does not execute permissions, attention to the PHP user to change the permissions that is nobody the user's permissions.

The following is my own code, PHP beginners Soon, not concise enough for reference only
The file conversion function in the YII framework, Doc transfer SWF

File conversions
Oscar 2011-11-25
Class Converter extends cwidget{

Public function run ($file)

$FILESARR = Array (' PDF ', ' Doc ', ' docx ', ' xls ', ' xlsx ', ' ppt ', ' pptx ', ' txt ');
File Upload and Convert
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 a time stamp
$types [0] = time ();
$fileName = $types [0]. $types [1];
$filetype = $typesIf;

restricting upload types
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);
Detect Chinese file name
if (Check_is_chinese ($fileName)) {
$types [0] = time ();
$fileName = $types [0]. $types [1];

Change path
if ($typesIf = = ' pdf ') {
$path = ' converter/files/pdf/';
$path = ' converter/files/doc/';

if (File_exists ($path. $fileName)) {
Echo ' <script type= "Text/javascript" >alert ("File already exists");location.href=location.href;</script> ';
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)) {
Perform a transformation
if ($typesIf = = ' pdf ') {//pdf to 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 ');
$path 2 = $pdfpath. $pdf;
$path 3 = $swfpath. $swf;
}else{//doc Transfer 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);
$path 1 = $docpath. $doc;
$path 2 = $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=/u Sr/share/xpdf/xpdf-chinese-simplified-s flashversion=9 ';
EXEC ($swfcommand);
$path 3 = $swfpath. $swf;

Print_r ($_files["file"]);
$filetype = Isset ($filetype)? $filetype: ';
$path 1 = isset ($path 1)? Str_replace ('/data/oa/frontend/www ', ', $path 1): ';
$path 2 = isset ($path 2)? Str_replace ('/data/oa/frontend/www ', ', $path 2): ';
$path 3 = isset ($path 3)? Str_replace ('/data/oa/frontend/www ', ', $path 3): ';
$REARR = Array (' filetype ' => $filetype, ' path1 ' => $path 1, ' path2 ' => $path 2, ' Path3 ' => $path 3);
return $REARR;
} php100 Chinese web


deleting files
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);


Related Article

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: 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.