如何在 PHP 中将 HTML 转换为 RTF、DOC 或 DOCX?

发布于 2024-12-03 16:23:55 字数 1536 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃梦幻 2024-12-10 16:23:55

我的选择是 https://packagist.org/packages/phpoffice/phpword

1 行安装:

composer require phpoffice/phpword

并且您可以将 HTML 转换为 RTF 或 DOC:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('Heading2', ['alignment' => 'center']);    
$section = $phpWord->addSection();
$html = '<h1>Adding element via HTML</h1>';
$html .= '<p>Some well-formed HTML snippet needs to be used</p>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);

https://github.com/PHPOffice/PHPWord/blob/08064480ea915a7283a5a09ba68ad04869cb259e/samples/Sample_26_Html.php

这是如何使用 Symfony 框架进行样式设置的详细文章:
https: //ourcodeworld.com/articles/read/361/how-to-create-a-word-file-with-php-in-symfony-3

PS:

以下是其他可能选项的列表:
https://www.w3.org/Tools/html2things.html

My choice is https://packagist.org/packages/phpoffice/phpword

1 line to install:

composer require phpoffice/phpword

And you can convert HTML to RTF or DOC:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('Heading2', ['alignment' => 'center']);    
$section = $phpWord->addSection();
$html = '<h1>Adding element via HTML</h1>';
$html .= '<p>Some well-formed HTML snippet needs to be used</p>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);

https://github.com/PHPOffice/PHPWord/blob/08064480ea915a7283a5a09ba68ad04869cb259e/samples/Sample_26_Html.php

Here is detailed article how to do styling using Symfony framework:
https://ourcodeworld.com/articles/read/361/how-to-create-a-word-file-with-php-in-symfony-3

P.S.:

Here is a list of other possible options:
https://www.w3.org/Tools/html2things.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文