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

How to convert html to word in php?

Jul 11, 2020 pm 03:22 PM
html php word

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 = '', $fileName = '')
{
    if (empty($content)) {
        return;
    }
    $content='<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" />'.$content.'</html>';
    if (empty($fileName)) {
        $fileName = date('YmdHis').'.doc';
    }
    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=''){
    if(empty($content)){
        return;
    }
    if (empty($fileName)) {
        $fileName = date('YmdHis').'.doc';
    }    // 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 = '<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">';
    $html .= '<head><meta http-equiv="Content-Type" content="text/html;charset="UTF-8" /></head>';
    echo $html . '<body>'.$content .'</body></html>';
}
createWord(file_get_contents('html2word.html'));
downloadWord(file_get_contents('html2word.html'));
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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

HTML Table Layout

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles