search
Homephp教程php手册php在程序中把网页生成word文档并提供下载 - jerrylsxu

在这篇文章中主要解决两个问题:

   1:在php中如何把html中的内容生成到word文档中

   2:php把html中的内容生成到word文档中时,不居中显示问题,即会默认按照web视图进行显示。

   3:php把html中的内容生成到word文档中时,相关样式不兼容问题

正文:

   echo '
             


                   
                   Print
                   
            ';

echo '


    ";
echo '
';
    echo "数字化教学系统电子备课稿

        学科 语文学校 实验中学
    
';

                ob_start(); //打开缓冲区 
 header("Cache-Control: public");
Header("Content-type: application/octet-stream"); 
Header("Accept-Ranges: bytes"); 

if (strpos($_SERVER["HTTP_USER_AGENT"],'MSIE')) {
  header('Content-Disposition: attachment; filename=test.doc');
  }else if (strpos($_SERVER["HTTP_USER_AGENT"],'Firefox')) {
  Header('Content-Disposition: attachment; filename=test.doc'); 
  } else {
  header('Content-Disposition: attachment; filename=test.doc');
  }
  header("Pragma:no-cache");
header("Expires:0");
ob_end_flush();//输出全部内容到浏览器 

注:以上代码部分提供了在php程序文件中生成内容到word文档中并提供下载功能。

       针对第2个问题,下载到本地的word文档打开后显示默认按照web视图进行显示的问题:如下图:

     按照默认web视图显示:

 

如果按照正常的页面视图进行显示的话,需要在头部添加一行xml标示进行设置(蓝色代码部分):Print,添加后下载到本地的word文档打开后显示如下图:

 

针对第三个问题就是有一些样式不兼容问题,比如顶部的大标题下面的相关属性的下划线标注显示:

  我们在html中的样式中添加了border-bottom: 1px solid #545454;这个样式(蓝色代码部分),即:,但是下划线还是没有显示,因为在word中不识别。如下图:

 

解决方法就是按照word识别的下标样式进行更改,即:,更改为这个样式后,即在下载到本地的word文档打开后就有下划线标示显示了。

 

授之于鱼,不如授之于渔,我把我的关于这个解决样式不兼容的解决方法给大家分享一下:

   一:找个web版在线编辑器,然后在里面随便输入几个文字,然后添加下划线标示

   二:然后点击编辑器上面的查看源代码按钮,可以看到刚才添加的那个下划线标示的属性即为text-decoration: underline;而不是html中的样式标示:border-bottom: 1px solid #545454;

   好了,针对以上相关问题就到这里,如有问题请大家提出来,我们共同讨论解决哈。

Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software