我正在尝试使用包含内部css
样式和HTML标签属性的HTML代码生成docx文件并下载给用户。
这段HTML代码是使用docx2html
包生成的,它可以(听起来)允许我们将上传的docx文件转换为HTML,所以我想做相反的过程:
<p><meta charset=utf-8><meta key="generator" value="docx2html">docx file titel
<style type="text/css">#A * { margin: 0px; border: 0px; padding: 0px; box-sizing: border-box; }
#A table { width: 100%; border-collapse: collapse; word-break: break-word; }
#A section { margin: auto; background-color: white; color: black; position: relative; z-index: 0; }
#A p:empty::before { content: ""; display: inline-block; }
#A ul { list-style: none; }
#A ul > li > p { position: relative; }
#A ul .marker { position: absolute; }
#A a { text-decoration: none; }
#A .unsupported { outline: red solid 2px; }
#A .warning { outline: yellow solid 1px; }
#A p, #A h1, #A h2, #A h3, #A h4, #A h5, #A h6 { margin-bottom: 10px; line-height: 107.917%; }
#A span, #A a { font-family: Calibri; font-size: 11px; }
#A .Normal { margin-bottom: 13px; line-height: 115%; }
#A .Normal span { }
#A .Policepardfaut { }
#A .TableauNormal { }
#A .TableauNormal > tbody > tr > td { padding: 0px 7px; }
#A .Default { line-height: 100%; }
#A .Default span { font-family: Calibri; color: rgb(0, 0, 0); font-size: 12px; }
</style>
</p>
<div id="A" style="background-color: transparent; min-height: 1000px; width: 100%; padding-top: 20px; overflow: auto;">
<style type="text/css">
</style>
<section style="width: 793px; min-height: 1122px; padding: 94px; column-gap: 47px;">
<p class="Normal" style="line-height: 100%; text-align: right;"><span class="Policepardfaut" style="font-family: Calibri; font-weight: 700; color: rgb(0, 0, 0); font-size: 14px;">Alger, </span><span class="Policepardfaut" style="font-family: Calibri; font-weight: 700; color: rgb(0, 0, 0); font-size: 14px;">Le </span><span class="Policepardfaut" style="font-family: Calibri; color: rgb(0, 0, 0); font-size: 14px;">25</span><span class="Policepardfaut" style="font-family: Calibri; color: rgb(0, 0, 0); font-size: 14px;">/07/2018</span></p>
</section>
</div>
答案 0 :(得分:0)
PHPWord
PHPWord是一个不错的库,您可以使用它来读取和写入不同的格式。它还支持读取html和写入docx。 也可以使用HTML样式。
https://github.com/PHPOffice/PHPWord
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = '<h1>Adding element via HTML</h1>';
$html .= '<p>Some well-formed HTML snippet needs to be used</p>';
$html .= '<h2 style="align: center">centered title</h2>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('example.docx');
php替代。
您也可以尝试这里的建议-这种方法不需要任何库->
https://stackoverflow.com/a/43380995/5687225
javascript替代。
如果您想通过JavaScript进行操作,则可以使用以下软件包pt-html-docx-js
。
https://www.npmjs.com/package/pt-html-docx-js
您将像这样使用它
var converted = htmlDocx.asBlob(content, {orientation: 'landscape', margins: {top: 720}});
saveAs(converted, 'test.docx');