index.php中的代码
<?php
$html = file_get_contents( $_REQUEST['fname']);
$filename = 'form_' . uniqid () . '.doc';
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename= . $filename");
echo $html;
?>
html文件的路径
form_588f71c4e978d.html
运行后
http://localhost/html2/html2wordf.php?fname=form_588f71c4e978d.html
我得到以下代码
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title></title>
</head>
<body>
<p>hello</p>
</body>
</html>
里面
form_588f71c4e978d.doc
但我只想要hello
在这个文件里面
表示html to doc数据转换不起作用,它只是将其扩展名从.html更改为.doc,文件内部的数据保持不变
任何一个得到我的观点?指导我,谢谢
答案 0 :(得分:1)
有很好的PHP库,但它不是免费的: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP
如果您只想替换单词模板中的一些文本(我建议您使用docx格式)您可以解压缩docx文件,您将找到XML文件此内容。
所以,你可以使用str_replace(&#39; {{youVariableInWordTemplate}}&#39;,$ value,$ wordXML);
另一种方式:使用PhpWord
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = '<p><strong>You html here</strong></p>';
Html::addHtml($section, $html);
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$cacheDir = '/temp_directory_of_your_project/';
$objWriter->save($cacheDir. 'helloWorld.docx');
但是这个库有表生成的问题。问题已解决:Using table tag in HTML Reader produces no output有使用自定义类的解决方案(请参阅帖子中的附件)
您还可以找到改进的实施方式:HTML Reader from PHPWord does't work with tables?
这个库支持的HTML标签不多(下面支持标签的数组):
$nodes = array(
// $method $node $element $styles $data $argument1 $argument2
'p' => array('Paragraph', $node, $element, $styles, null, null, null),
'h1' => array('Heading', null, $element, $styles, null, 'Heading1', null),
'h2' => array('Heading', null, $element, $styles, null, 'Heading2', null),
'h3' => array('Heading', null, $element, $styles, null, 'Heading3', null),
'h4' => array('Heading', null, $element, $styles, null, 'Heading4', null),
'h5' => array('Heading', null, $element, $styles, null, 'Heading5', null),
'h6' => array('Heading', null, $element, $styles, null, 'Heading6', null),
'#text' => array('Text', $node, $element, $styles, null, null, null),
'span' => array('Span', $node, null, $styles, null, null, null), //to catch inline span style changes
'strong' => array('Property', null, null, $styles, null, 'bold', true),
'em' => array('Property', null, null, $styles, null, 'italic', true),
'sup' => array('Property', null, null, $styles, null, 'superScript', true),
'sub' => array('Property', null, null, $styles, null, 'subScript', true),
'table' => array('Table', $node, $element, $styles, null, 'addTable', true),
'tbody' => array('Table', $node, $element, $styles, null, 'skipTbody', true), //added to catch tbody in html.
'tr' => array('Table', $node, $element, $styles, null, 'addRow', true),
'td' => array('Table', $node, $element, $styles, null, 'addCell', true),
'ul' => array('List', null, null, $styles, $data, 3, null),
'ol' => array('List', null, null, $styles, $data, 7, null),
'li' => array('ListItem', $node, $element, $styles, $data, null, null),
);