【问题标题】:HTML to DOCX file with php使用 php 将 HTML 转换为 DOCX 文件
【发布时间】:2023-03-05 13:01:01
【问题描述】:

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 到 doc 的数据转换不起作用,它只是将扩展名从 .html 更改为 .doc 并且两个文件中的数据保持不变

有人明白我的意思吗?指导我谢谢

【问题讨论】:

  • 谁告诉你这个 sn-p 会将 HTML 转换为正确的 Word 文件?它只是发送一个虚假的 Content-Type。 (除非有其他代码你没有显示。)
  • 表示 html 到 doc 的数据转换不工作 当然...没有调用调用
  • 文件中的HTML标记既不是doc,也不是docx;它只是具有不同扩展名的文件中的 html 标记
  • 仅仅设置一个header并不会神奇地把它变成一个word文件
  • 考虑看看像 PHPOffice/PHPWord 这样的库,它可以将 html 标记转换为真正的 BIFF (doc) 或 OfficeOpenXML (docx) 格式文件

标签: php


【解决方案1】:

有很好的 PHP 库,但它不是免费的: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP

如果你只是想在你的word模板中替换一些文本(我建议你使用docx格式)你可以解压docx文件,你会找到XML文件这个内容。

所以,你可以使用 str_replace('{{youVariableInWordTemplate}}', $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),
    );

【讨论】:

  • 那么这个书目中的 inined 样式甚至不起作用!
猜你喜欢
  • 2011-06-03
  • 1970-01-01
  • 2018-08-28
  • 2020-04-18
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多