php 如何使用php将html转换为word?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40628171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert html to word using php?
提问by Ishaque Javed
I am trying to convert HTML to MS WORD document (.doc/.docx) with PHP script. With the available scripts from internet I am able to convert the text of HTML to doc. But I need total html with inline css will be in my doc.I made one script
我正在尝试使用 PHP 脚本将HTML转换为 MS WORD 文档 (.doc/.docx)。使用 Internet 上的可用脚本,我可以将 HTML 文本转换为 doc。但我需要内联 css 的总 html 将在我的文档中。我制作了一个脚本
$html = file_get_contents('html path');
$tags = "<br>";
$test = strip_tags($page,$html);
$breaks = array("<br />","<br>","<br/>");
$text = str_ireplace($breaks, "\r\n", $test);
$text = iconv('UTF-8', 'ASCII//TRANSLIT',$text);
$handle = fopen("newdoc.doc", "w+");
fwrite($handle, $text);
fclose($handle);
It's working for the text content of HTML only. But I can't add images to it is there any way to do it ? Please Help, Thanks in advance.
它仅适用于 HTML 的文本内容。但是我不能向它添加图像有什么办法吗?请帮助,提前致谢。
回答by SAR
just keep following code in top of the page need to convert:
只需将以下代码保留在需要转换的页面顶部:
<? header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=Report.doc");
?>
回答by Kitson88
I would highly recommend PHPdocxas it does exactly what your asking for, but at a cost. I purchased it sometime ago and use it frequently.
我强烈推荐PHPdocx,因为它完全符合您的要求,但需要付出代价。我前段时间购买了它并经常使用它。
require_once 'pathToPHPDocX/classes/CreateDocx.inc';
$docx = new CreateDocx();
$html='http://www.2mdc.com/PHPDOCX/simpleHTML_Image.html';
$docx->embedHTML($html, array('isFile' => true, 'downloadImages' => true));
$docx->createDocx('simpleHTML');
http://www.phpdocx.com/documentation/introduction/html-to-word-PHP
http://www.phpdocx.com/documentation/introduction/html-to-word-PHP
回答by Dave L
I am currently working through something similar and have gone with
我目前正在处理类似的事情,并且已经完成了
Huge amount of information on the site to get you started and you can style each element how you would like.
网站上的大量信息可以帮助您入门,您可以根据自己的喜好设置每个元素的样式。
Images can be added simply with: $section->addImage($src, [$style]);
可以简单地添加图像: $section->addImage($src, [$style]);
I would take a look. Its a bit more involved than what you are currently using, but allows you to do what you need.
我去看看。它比您目前使用的要复杂一些,但允许您做您需要的事情。
回答by lokeshkgupta
I would highly recommend HTML_TO_DOC Class for Convert HTML to MS Word Document using PHP
我强烈推荐 HTML_TO_DOC 类使用 PHP将HTML转换为 MS Word 文档
You can use this Code for Header generation.
您可以使用此代码生成标题。
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">