douwang4374 2016-07-02 13:15
浏览 58

使用PHP在docx文档中搜索和替换POST变量

I am trying to do a search and replace on a Microsoft Word docx document. The odd problem I am having is that, of the 7 posted vars, 1 and 5 do not replace. In checking Chrome Tools, they all appear in the posted form data. The names are all spelled correctly too.

To explain this code, I am opening a template doc, copying it to another name (the named file which will be downloaded), replacing text in document.xml, then replacing text in the footer. Then I close the file and download it. It downloads, except, the vars $pname and $hca2name do not replace inside of the document. All other vars are properly replaced.

Any thoughts are very much appreciated. Thank you. Here is the code:

<?php
$pname = (string) $_POST['pname'];
$countyres = (string) $_POST['countyres'];
$a1name = (string) $_POST['a1name'];
$a2name = (string) $_POST['a2name'];
$hca1name = (string) $_POST['hca1name'];
$hca2name = (string) $_POST['hca2name'];
$atty = (string) $_POST['atty'];
$countyres = (string) $_POST['countyres'];
$fn = 'POA-'.$pname.'.docx';
$s = "docs_archive/PA-poas2no.docx";
$wordDoc = "POA-".$pname.".docx";
copy($s, $wordDoc);
$zip = new ZipArchive;
//This is the main document in a .docx file.
$fileToModify = 'word/document.xml';
if ($zip->open($wordDoc) === TRUE) {
    //Read contents into memory
    $oldContents = $zip->getFromName($fileToModify);
    //Modify contents:
    $newContents = str_replace('{pname}', $pname, $oldContents);
    $newContents = str_replace('{a1name}', $a1name, $newContents);
    $newContents = str_replace('{a2name}', $a2name, $newContents);
    $newContents = str_replace('{hca1name}', $hca1name, $newContents);
    $newContents = str_replace('{hca2name}', $hca2name, $newContents);
    $newContents = str_replace('{atty}', $atty, $newContents);
    $newContents = str_replace('{countyres}', $countyres, $newContents);
    //Delete the old...
    $zip->deleteName($fileToModify);
    //Write the new...
    $zip->addFromString($fileToModify, $newContents);
    //Open Footer and change vars there
    $ft = 'word/footer1.xml';
    $oldft = $zip->getFromName($ft);
    $newft = str_replace('{pname}', $pname, $oldft);
    $zip->deleteName($ft);
    $zip->addFromString($ft, $newft);
    $zip->close();
header('Content-Description: File Transfer');
header("Content-Type: application/force-download");
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="'.$fn.'"');
header('Content-Transfer-Encoding: binary');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
readfile($fn);
unlink($fn);
exit();
}
?>
  • 写回答

2条回答 默认 最新

  • duan19913 2016-07-03 09:57
    关注

    For the benefit of any future reader experiencing a similar problem, when I analyzed the document.xml file within the zipped docx file, my variable names were often not contiguous instead of {pname} it would be seperated into { pname

    Open you main document template, got to File, Inspect Document, Check for issues. This will allow you to clean up the file and remove much of that intervening xml which, I assume, is some document tracking data.

    It worked for me.

    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备