【问题标题】:How to generate TOC from HTML in phpword如何在 phpword 中从 HTML 生成 TOC
【发布时间】:2020-11-06 21:19:51
【问题描述】:

我正在使用 PHPWord 库将我的 HTML 文档转换为 Docx 文件。

我在 Word 文档中正确生成 TOC(目录)时遇到问题。 在 HTML 中,TOC 是通过 # 将锚标记与 div 链接创建的,并且可以完美运行。如何使用 PHPWord 将其转换为 Docx TOC?

我生成 HTML 到 Docx 的代码如下:

        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();        
        \PhpOffice\PhpWord\Shared\Html::addHtml($section,$htmlContent);
        $targetFile = __DIR__ . "/convertedFile.docx";
        $phpWord->save($targetFile, 'Word2007');

库链接: PHPWord

【问题讨论】:

    标签: phpword tableofcontents


    【解决方案1】:
     <form method="post" action="gendocx.php">
     <input type="hidden" name="htmlstring" id="htmlstring" value="">
     <input type="submit" id="MyButton" value="Generate Docx">
     </form>
     ...
     <script>
     $('#myButton').click(function(){
     //get your Div HTML trying
     var s = $('#myHTMLDiv').html();
     $('#htmlstring').val(s);
     });
     </script>
    
    
    Then in gendocx.php we’ll take the incoming HTML string and put it into Docx document 
    like this.
    
    require_once 'vendor/autoload.php';
    
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    \PhpOffice\PhpWord\Shared\Html::addHtml($section, $_POST['htmlstring']);
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename="test.docx"');
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('php://output');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 2016-01-10
      • 2013-04-03
      • 2011-08-14
      • 2011-08-04
      • 1970-01-01
      • 2020-03-07
      相关资源
      最近更新 更多