2014-10-20 65 views
0

我正在致力于建立一个基于所需核心程序的质量,安全或环境管理系统的客户项目,有3个选项,因此有7个不同的组合。我编写了一个代码,查看他们需要的内容,然后将数据插入到其客户端文档表中,其中包括模板文档的副本。代码本身工作得很好,并成功地为客户端构建了一个文档列表,并插入了模板文档(MS Word)。编辑phpcopy后的工作文档

我需要什么帮助,我不知道这是可能的,是找到一种方法,一旦他们被复制整个编辑word文档。

模板位于我的/templates文件夹中,然后复制到/clientfolder,我用来查询,追加和复制的代码如下所示,我知道这并不完美,我将尽快更新它,但它确实有效。

我需要的是内环路包括,编辑刚刚被放置在文档,并插入公司名称和公司标志,这是举行我的项目中的会话变量代码。如果您在这里查看此示例表(http://iso-drive.co.uk/public/templates_documents_list.php),您可以打开一个文档文档,它具有两个文本区域---> [公司名称] [徽标]。

所以基本上我需要编辑每个Word文档,然后插入两个$ _Sessopm [公司名称]并在$ _SESSION [CompanyLogo的。

//Add Template Documentation 
//step 1 identify which documentation set is required 
$sql = "SELECT * FROM templates_documents WHERE scope = '".$values["msscope"]."'"; 
$rs = CustomQuery($sql); 
$results = $rs; 
while ($row = $results->fetch_assoc()) 
{ 
//Step 2 correctly name doc reference 
if ($row["scope"] == '9001') $docref = $row["9001ref"]; 
elseif ($row["scope"] == '14001') $docref = $row["14001ref"]; 
elseif ($row["scope"] == '18001') $docref = $row["18001ref"]; 
elseif ($row["scope"] == '9001,14001') $docref = $row["914001ref"]; 
elseif ($row["scope"] == '9001,18001') $docref = $row["918001ref"]; 
elseif ($row["scope"] == '14001,18001') $docref = $row["1418001ref"]; 
elseif ($row["scope"] == '9001,14001,18001') $docref = $row["91418001ref"]; 

//Step 3 Decode Attachment Field 
$fileArray = json_decode($row["attachment"],true); 

//values from Attachment Array 
$name1 = $fileArray["0"]["name"]; 
$usrName1 = $fileArray["0"]["usrName"]; 
$size1 = $fileArray["0"]["size"]; 
$type1 = $fileArray["0"]["type"]; 
$searchStr1 = $fileArray["0"]["searchStr"]; 

$name2 = $folder.'/'.$docref.'.docx'; 
$usrName2 = $docref.".docx"; 
$size2=$size1; 
$type2=$type1; 
$searchStr2 = $usrName2.",!:sStrEnd"; 
$attachment2 = '[{"name":"'.$folder.'\/'.$usrName2.'","usrName":"'.$usrName2.'","size":'.$size2.',"type":"'.$type2.'","searchStr":"'.$searchStr2.'"}]'; 

//Step 4 insert template documentation into main docs table 
$sql2 = "INSERT INTO documents_doucments (companyfk, docref, doctype, doctitle, revision, issuedate, status, reasonforchange, attachment) 
values('".$values["companypk"]."', '$docref', '$row[doctype]','$row[templatename]','0.1', now(),1, 'First Draft', '$attachment2')";CustomQuery($sql2); 

//step 4 copy and rename attachment into main docs table 
if ($fileArray){ 
$originalfile = $name1; 
$newfile = $name2; 
copy($name1, $name2);} 

//step 5 insert duplicate records from 3 and 4 above into document history (child) table 
//get docpk from master table 
$sql3 = "SELECT * FROM documents_doucments where companyfk = '".$values["companypk"]."' and docref = '$docref' "; 
$rs3 = CustomQuery($sql3); 
$results3 = $rs3; 
while ($row = $results3->fetch_assoc()) 
{ 
$documentpk = $row["documentpk"]; 

//insert 
$sql4 = "INSERT INTO documents_documenthistory (documentfk, docref, doctype, doctitle, revision, issuedate, status, reasonforchange, attachment) values('$documentpk', '$docref', '$row[doctype]','$row[templatename]','0.1', now(),1, 'First Draft', '$attachment2')";CustomQuery($sql4); 


} } 

回答

0

如果您希望编辑从PHP水平Word文档,则需要外部库来对付它,因为Word文件的二进制格式和普通的PHP函数,如substr()将没有多大用处的这里。

看看PHPWord,这对于在PHP编辑Word文档的唯一目的,特别是存在的。

如果在另一方面,你要的文件呈现给人类用户,请编辑和上传的文件回来,这是一个完全不同的故事。为了达到这个目的,你需要像readfile()file upload mechanisms这样的功能。作为一个方面说明,我建议将代码拆分为方法并使用一致的缩进,因为您发布的代码几乎不可读。

+0

谢谢,PHPWord可以编辑现有文档吗?从我读过的内容来看,它是用来在飞行中创建新的? – 2014-10-20 09:34:51

+0

'$ template = $ phpWord-> loadTemplate('Template.docx');'?看起来很有希望。 – 2014-10-20 09:36:56