在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

时间:2024-02-16 19:07:52

条件:

  1. 安装好禅道的服务器
  2. 能直接浏览PDF的浏览器(或通过 安装插件实现 )
  3. 文档转换服务程序(建议部署在另一台服务器上)

   

实现 原理:

  1. 修改禅道的文件预览功能(OFFICE文档其使用的是下载打开方式)为向转换服务请求PDF
  2. 转换服务收到URL后,下载该文件并调用后台服务转换为PDF,向请求者返回PDF
  3. 禅道内置的预览PDF功能直接请求浏览器预览PDF

   

需要修改的文件及内容

xampp\zentao\module\file\control.php

xampp\zentao\module\file\view\printfiles.html.php

xampp\zentao\module\group\lang\resource.php

xampp\zentao\module\file\lang\zh-cn.php

   

control.php

   

增加方法

   

public function cloudview($fileID, $mouse = \'\')

{

$file = $this->file->getById($fileID);

   

/* Judge the mode, down or open. */

$mode = \'view\';

$fileTypes = \'txt|jpg|jpeg|gif|png|bmp|xml|html|pdf|dwg\';

if(stripos($fileTypes, $file->extension) !== false and $mouse == \'left\') $mode = \'open\';

        if($mode == \'open\')

{

if(file_exists($file->realPath))$this->locate($file->webPath);

$this->app->triggerError("The file you visit $fileID not found.", __FILE__, __LINE__, true);

}

        else

if(file_exists($file->realPath))

{

$fileName = $file->title . \'.\' . $file->extension;

                $fileData = file_get_contents($file->realPath);

                $md5data=md5( $fileData);

$this->locate( \'http://192.168.118.136:9345/?url=\' . common::getSysURL() . $file-> webPath . \'&\' . \'fileName=\' . $fileName . \'&\' . \'md5=\' . $md5data );

}

else

{

$this->app->triggerError("The file you visit $fileID not found.", __FILE__, __LINE__, true);

}

   

}

   

   

printfiles.html.php

   

修改方法downloadFile

新增方法viewFile

增加超链接指向

function downloadFile(fileID)

{

if(!fileID) return;

var sessionString = \'<?php echo $sessionString;?>\';

var url = createLink(\'file\', \'cloudview\', \'mfileID=\' + fileID + \'&mouse=left&mode=open\') + sessionString;

      

window.open(url, \'_blank\');

return false;

}

   

function viewFile(fileID)

{

if(!fileID) return;

var sessionString = \'<?php echo $sessionString;?>\';

var url = createLink(\'file\', \'cloudview\', \'mfileID=\' + fileID + \'&mouse=left&mode=open\') + sessionString;

        

window.open(url, \'_blank\');

return false;

}

   

增加链接

   

$fileTitle = "<li class=\'list-group-item\'><i class=\'icon-file-text text-muted icon\'></i> &nbsp;" . $file->title .\'.\' . $file->extension;

echo html::a($this->createLink(\'file\', \'download\', "fileID=$file->id") . $sessionString, $fileTitle, \'_blank\', "onclick=\'return viewFile($file->id)\'");

echo "<span class=\'right-icon\'>";

         common::printLink(\'file\', \'download\', "fileID=$file->id", "<i class=\'icon-download\'></i>", \'\', "class=\'edit btn-icon\' title=\'{$lang->file->download}\'");

common::printLink(\'file\', \'edit\', "fileID=$file->id", "<i class=\'icon-pencil\'></i>", \'\', "class=\'edit btn-icon\' title=\'{$lang->file->edit}\'");

if(common::hasPriv(\'file\', \'delete\')) echo html::a(\'###\', "<i class=\'icon-remove\'></i>", \'\', "class=\'btn-icon\' onclick=\'deleteFile($file->id)\' title=\'$lang->delete\'");

echo \'</span>\';

echo \'</li>\';

   

resource.php

   

$lang->resource->file = new stdclass();

$lang->resource->file->download = \'download\';

$lang->resource->file->cloudview = \'cloudview\';

$lang->resource->file->edit = \'edit\';

   

zh-cn.php

   

$lang->file->uploadImages = \'多图上传\';

$lang->file->cloudview = \'云预览\';

$lang->file->download = \'下载附件\';

   

   

执行效果

   

权限

   

   

附件管理