将内容从文档复制到具有相同格式的新文档中

时间:2018-10-16 00:58:27

标签: javascript php jquery html

现在,我有一个文档,几乎就像一个带有粗体,缩进,标题的模板,我希望能够获取该文档的内容,并用用户以我设置的形式输入的内容替换某些字符串起来我有它工作的地方,它使一个新的,并用纯文本文档替换单词,而无需特殊格式。有没有办法保持所有格式?无论如何,它都是很酷的,但是我主要使用PHP,到目前为止,我要做的代码是PHP。

这是无需格式化即可运行的代码。

<?php  
session_start();


$file = tempnam(sys_get_temp_dir(), 'TMP_');

file_put_contents($file, file_get_contents("freewillben1.docx"));

  //replaces string in document with data 
$fh = fopen($file, 'a') or die("can't open file");
$placeholders = array('Fff', 'Mmm','Lll');
$namevals = 
array($_SESSION["fName"],$_SESSION["mInitial"],$_SESSION["lName"]);
$path_to_file = 'freewillben1.docx';
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace($placeholders,$namevals,$file_contents);
file_put_contents($file,$file_contents);
fclose($fh);


if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");

$type = filetype($file);
// Get a date and timestamp
$today = date("F j, Y, g:i a");
$time = time();
// Send file headers
header("Content-type: application/vnd.openxmlformats- 
officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=yourwill.docx");
header("Content-Transfer-Encoding: binary"); 
header('Pragma: no-cache'); 
header('Expires: 0');
// Send the file contents.
set_time_limit(0); 
readfile($file);
?>

0 个答案:

没有答案