Word转SWF整体流程概述
相关实现代码如下:
要实现类似百度文库的功能,需要进行一系列格式转换。一般流程是先将Word转换为PDF格式,再将PDF转换为SWF格式。在网页端实际显示的均为SWF格式的内容。
第一步是将Word文件转换为SWF格式。这需要调用COM组件,通常可通过Office或WPS进行转换,但尝试后均未成功。最终采用OpenOffice 4.0.0版本才顺利完成转换。OpenOffice 4.0.0兼容Windows和Linux系统,因此需先下载并安装该软件(官网可获取)。需要注意的是,直接转换时似乎仅支持英文标题的文档,不支持中文标题。解决方法是先将文件重命名为英文,完成转换后再改回中文名称即可。此外,可能还需要启动OpenOffice服务,具体操作见上图。
验证服务是否已成功开启,参考上图所示。
启动服务的代码如下:
复制相关代码。
具体的转换逻辑代码如下:
<?php
class RunTime//页面执行时间类
{
private $starttime;//页面开始执行时间
private $stoptime;//页面结束执行时间
private $spendtime;//页面执行花费时间
function getmicrotime()//获取返回当前微秒数的浮点数
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function start()//页面开始执行函数,返回开始页面执行的时间
{
$this->starttime=$this->getmicrotime();
}
function end()//显示页面执行的时间
{
$this->stoptime=$this->getmicrotime();
$this->spendtime=$this->stoptime-$this->starttime;
//return round($this->spendtime,10);
}
function display()
{
//$this->end();
echo "运行时间:".round($this->spendtime,10)."秒
";
}
}
/*调用方法 */
$timer=new Runtime();
$timer->start();
function MakePropertyValue($name,$value,$osm){
$oStruct = $osm->Bridge_GetStruct
("com.sun.star.beans.PropertyValue");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
}
function word2pdf($doc_url, $output_url){
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
$args = array(MakePropertyValue("Hidden",true,$osm));
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
$oWriterDoc = $oDesktop->loadComponentFromURL
($doc_url,"_blank", 0, $args);
$export_args = array(MakePropertyValue
("FilterName","writer_pdf_Export",$osm));
$oWriterDoc->storeToURL($output_url,$export_args);
$oWriterDoc->close(true);
}
$output_dir = "C:/";
$doc_file = "C:/t.doc";
$pdf_file = "9.pdf";
$output_file = $output_dir . $pdf_file;
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;
word2pdf($doc_file,$output_file);
$timer->end();
$timer->display();
?> 对处理时间进行分析如下:
在一个大小为1.48MB的Word文档转换为PDF的过程中,运行时间为1.3652579784秒。此为个人电脑上的测试结果。
第二步是将PDF文件转换为SWF。这需要借助另一个软件Swftools,通过代码调用CMD命令来实现,直接提供代码如下。
<?php
class RunTime//页面执行时间类
{
private $starttime;//页面开始执行时间
private $stoptime;//页面结束执行时间
private $spendtime;//页面执行花费时间
function getmicrotime()//获取返回当前微秒数的浮点数
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
function start()//页面开始执行函数,返回开始页面执行的时间
{
$this->starttime=$this->getmicrotime();
}
function end()//显示页面执行的时间
{
$this->stoptime=$this->getmicrotime();
$this->spendtime=$this->stoptime-$this->starttime;
//return round($this->spendtime,10);
}
function display()
{
//$this->end();
echo "运行时间:".round($this->spendtime,10)."秒
";
}
}
/*调用方法 */
$timer=new Runtime();
$timer->start();
//调用系统软件
$command = "\"C:\Program Files\SWFTools\pdf2swf.exe\" -t C:\8.pdf -s flashversion=9 -o C:\m.swf";
echo $command;
exec($command);
echo 'ok';
$timer->end();
$timer->display();
?>将之前生成的PDF文件转换为SWF文件需要使用以下命令或代码片段。
PDF转SWF的运行时间为:1.3119211197秒。最后一步是在网页中展示SWF文件。这一步骤需要引入多个JS文件及其他资源,由于代码较多,此处不再赘述,可直接在我的上传资料中下载相关文件。