Home > Backend Development > PHP Problem > How to convert html to word in php?

How to convert html to word in php?

coldplay.xixi
Release: 2023-03-02 12:28:01
Original
4737 people have browsed it

php method to convert html to word: 1. Generate word through mnt media, the code is [composer require cshaptx4869/html2word]; 2. Write the html file directly into word, and convert the image to base64 format.

How to convert html to word in php?

How to convert html to word using php:

1. Generate word

through the medium of mnt
composer require cshaptx4869/html2word
Copy after login
<?php
/**
 * @desc 方法一、生成word文档
 * @param $content
 * @param string $fileName
 */
function createWord($content = &#39;&#39;, $fileName = &#39;&#39;)
{
    if (empty($content)) {
        return;
    }
    $content=&#39;<html 
            xmlns:o="urn:schemas-microsoft-com:office:office" 
            xmlns:w="urn:schemas-microsoft-com:office:word" 
            xmlns="http://www.w3.org/TR/REC-html40">
            <meta charset="UTF-8" />&#39;.$content.&#39;</html>&#39;;
    if (empty($fileName)) {
        $fileName = date(&#39;YmdHis&#39;).&#39;.doc&#39;;
    }
    file_put_contents($fileName, $content);
}
Copy after login

2. Write the html file directly into word

Note: If there are pictures, convert them to base64 format

<?php/**
 * @desc 方法二、生成word文档并下载
 * @param $content
 * @param string $fileName
 */
function downloadWord($content, $fileName=&#39;&#39;){
    if(empty($content)){
        return;
    }
    if (empty($fileName)) {
        $fileName = date(&#39;YmdHis&#39;).&#39;.doc&#39;;
    }    // header("location:xxx.doc");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename={$fileName}");
    $html = &#39;<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:m="http://schemas.microsoft.com/office/2004/12/omml" 
         xmlns="http://www.w3.org/TR/REC-html40">&#39;;
    $html .= &#39;<head><meta http-equiv="Content-Type" content="text/html;charset="UTF-8" /></head>&#39;;
    echo $html . &#39;<body>&#39;.$content .&#39;</body></html>&#39;;
}
createWord(file_get_contents(&#39;html2word.html&#39;));
downloadWord(file_get_contents(&#39;html2word.html&#39;));
Copy after login

Related learning recommendations:Getting started with PHP programming To master

The above is the detailed content of How to convert html to word in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template