当前位置:网站首页>PHP export word method (one MHT)
PHP export word method (one MHT)
2022-07-03 11:59:00 【ignativs amor】
Related articles :
phpword Generate word file ,phpword Use
background
There are custom styles in the project , The function of exporting users' resumes , Resume , It is better to export word perhaps pdf Of , Finally, export to word
research
export word There are several ways , Today, let's talk about using MHT preservation word Methods
Use previous knowledge to supplement
1. What is? MHT
MHT, Its full name is mono html.MHTML Files are also called aggregations HTML file 、Web Archives or single document web pages . A single file web page can integrate all elements of the website ( Including text and graphics ) All saved to a single file . This encapsulation allows you to publish the entire website as a single inline MIME (MIME: adopt Internet A list of standards for connecting and delivering multimedia resources .MIME Type notifies what the program object contains ( As in Figure 、 Sound or video ) The aggregation of HTML file (MHTML) file , Or send the whole website as an email or attachment .Internet Explorer 4.0 And later versions support this format .
2. What is? HTML
HTML, Full name HyperTextMark-upLanguage, Hypertext markup language or hypertext link markup language , It is the most widely used language on the Internet , It is also the main language of Web documents . Design HTML The purpose of language is to connect the text or graphics stored in one computer with the text or graphics in another computer , Form an organic whole , People don't have to think about whether the specific information is on the current computer or other computers on the network . We just need to click an icon in a document with the mouse ,Internet It will go to the content related to this icon immediately , This information may be stored on another computer on the network . HTML Text is created by HTML Descriptive text composed of commands ,HTML Command descriptive text 、 graphics 、 Animation 、 voice 、 form 、 Links, etc. .HTML Structure including head (Head)、 The main body (Body) Two major parts , The header describes the information needed by the browser , And the subject contains the specific content to be explained
3. MHT And HTML The difference between
mht Format and html Is very similar , But in mht In the format , Externally linked files , Such as the picture 、Javascript、CSS Will be base64 Encode and store . therefore , Single mht File can save all resources in a web page , Of course , comparison html, Its size will also be relatively large .
- a * Essentially , Both of them , There's no difference , Is an intuitive web browsing format .
- b * Simple words , Their difference lies in preservation ,mht You can save everything you see directly ,html You can't save everything , Server side files cannot be saved locally , Need to rely on the network ,
4. understand word
word Its own function is still very powerful , It can open html File format , And the format can be preserved , Even if the suffix is doc, It can also recognize normal opening . This provides us with convenience .
But there's a problem ,html The image in the format file has only one address , Real pictures are stored elsewhere , in other words , If you will HTML Format to write doc in , that doc Will not contain pictures . So how do we create a doc Documents ? We can use and html Very close mht Format .
5. mht Format can be word Identify
Let's experiment , Open any web page , Save as mht file , Then change the suffix to doc, Reuse word open ,OK,word It can also identify mht file , And can display pictures .
since doc Can identify mht, The following is how to put the picture into mht 了 . because html The address of the image in the code is written in img Labeled src Properties of the , therefore , Just extract html In code src Property value , You can get the picture address . Of course , It is possible that what you get is a relative path , No problem , add URL The prefix of , Just change to an absolute path . With the picture address , We can go through file_get_content Function to get the specific content of the image file , And then call base64_encode Function encodes the contents of a file into base64 code , Finally insert into mht The proper location of the document is enough
Use
step 1. First render the page style
list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = self::ViewResume($iMedliveId);
$sHtmlContent = view('enterprise.resume.resume_doc',
compact('oResume','oIntension','oWorkexp','oEduexp','oAttach','title','updated_at'))
->render();
return $sHtmlContent;
step 2. according to HTML Code acquisition word Document content
Through the above principle introduction , Encapsulated as an export function , This function can take HTML The code is exported into a mht file , Parameters have 3 individual , After that 2 Are optional parameters :
- content: To convert HTML Code
- absolutePath: If HTML The image addresses in the code are relative paths , So this parameter is HTML The absolute path missing in the code .
- isEraseLink: Whether to remove HTML Hyperlinks in code
/** * according to HTML Code acquisition word Document content * Create one that is essentially mht Documents , This function will analyze the contents of the file and download the image resources in the page remotely * This function depends on the class MhtFileMaker * This function will analyze img label , extract src The attribute value . however ,src The attribute value of must be enclosed in quotation marks , Otherwise, you can't extract * param string $content HTML Content * param string $absolutePath The absolute path of the web page . If HTML The picture path in the content is a relative path , Then you need to fill in this parameter , To make the function automatically fill in the absolute path . This parameter needs to end with / end * param bool $isEraseLink Whether to remove HTML Links in content */
public static function getWordDocument( $content , $absolutePath = "" , $isEraseLink = false )
{
$mht = new MhtFileMaker();
if ($isEraseLink){
$content = preg_replace('/<a\s*.*?\s*>(\s*.*?\s*)<\/a>/i' , '$1' , $content); // Remove link
}
$images = array();
$files = array();
$matches = array();
// This algorithm requires src The attribute value after must be enclosed in quotation marks || preg_match_all('/url\s*\(([^\)]+)\)/i',$content ,$matches )
if ( preg_match_all('/<img.*?src="(.*?)".*?>/is',$content ,$matches)){
$arrPath = $matches[1];
for ( $i=0;$i<count($arrPath);$i++){
$path = $arrPath[$i];
$imgPath = trim( $path );
if ( $imgPath != "" ){
$files[] = $imgPath;
if( substr($imgPath,0,7) == 'http://'){
// Absolute link , Without prefixes
}else{
// $imgPath = $absolutePath.$imgPath;
$imgPath = public_path($imgPath);
}
$images[] = $imgPath;
}
}
}
$mht->AddContents("tmp.html",$mht->GetMimeType("tmp.html"),$content);
for ( $i=0;$i<count($images);$i++){
$image = $images[$i];
info($image);
if ( @fopen($image , 'r') ){
$imgcontent = @file_get_contents( $image );
if ( $content )
$mht->AddContents($files[$i],$mht->GetMimeType($image),$imgcontent);
}else{
return ;//echo "file:".$image." not exist!<br />";
}
}
return $mht->GetFile();
}
step 3. class MhtFileMaker
<?php
namespace App\Services;
/** *@author *@version 1.0 */
class MhtFileMaker{
var $config = array();
var $headers = array();
var $headers_exists = array();
var $files = array();
var $boundary;
var $dir_base;
var $page_first;
function MhtFile($config = array()){
}
function SetHeader($header){
$this->headers[] = $header;
$key = strtolower(substr($header, 0, strpos($header, ':')));
$this->headers_exists[$key] = TRUE;
}
function SetFrom($from){
$this->SetHeader("From: $from");
}
function SetSubject($subject){
$this->SetHeader("Subject: $subject");
}
function SetDate($date = NULL, $istimestamp = FALSE){
if ($date == NULL) {
$date = time();
}
if ($istimestamp == TRUE) {
$date = date('D, d M Y H:i:s O', $date);
}
$this->SetHeader("Date: $date");
}
function SetBoundary($boundary = NULL){
if ($boundary == NULL) {
$this->boundary = '--' . strtoupper(md5(mt_rand())) . '_MULTIPART_MIXED';
} else {
$this->boundary = $boundary;
}
}
function SetBaseDir($dir){
$this->dir_base = str_replace("\\", "/", realpath($dir));
}
function SetFirstPage($filename){
$this->page_first = str_replace("\\", "/", realpath("{$this->dir_base}/$filename"));
}
function AutoAddFiles(){
if (!isset($this->page_first)) {
exit ('Not set the first page.');
}
$filepath = str_replace($this->dir_base, '', $this->page_first);
$filepath = 'http://mhtfile' . $filepath;
$this->AddFile($this->page_first, $filepath, NULL);
$this->AddDir($this->dir_base);
}
function AddDir($dir){
$handle_dir = opendir($dir);
while ($filename = readdir($handle_dir)) {
if (($filename!='.') && ($filename!='..') && ("$dir/$filename"!=$this->page_first)) {
if (is_dir("$dir/$filename")) {
$this->AddDir("$dir/$filename");
} elseif (is_file("$dir/$filename")) {
$filepath = str_replace($this->dir_base, '', "$dir/$filename");
$filepath = 'http://mhtfile' . $filepath;
$this->AddFile("$dir/$filename", $filepath, NULL);
}
}
}
closedir($handle_dir);
}
function AddFile($filename, $filepath = NULL, $encoding = NULL){
if ($filepath == NULL) {
$filepath = $filename;
}
$mimetype = $this->GetMimeType($filename);
$filecont = file_get_contents($filename);
$this->AddContents($filepath, $mimetype, $filecont, $encoding);
}
function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){
if ($encoding == NULL) {
$filecont = chunk_split(base64_encode($filecont), 76);
$encoding = 'base64';
}
info($filecont);
$this->files[] = array('filepath' => $filepath,
'mimetype' => $mimetype,
'filecont' => $filecont,
'encoding' => $encoding);
}
function CheckHeaders(){
if (!array_key_exists('date', $this->headers_exists)) {
$this->SetDate(NULL, TRUE);
}
if ($this->boundary == NULL) {
$this->SetBoundary();
}
}
function CheckFiles(){
if (count($this->files) == 0) {
return FALSE;
} else {
return TRUE;
}
}
function GetFile(){
$this->CheckHeaders();
if (!$this->CheckFiles()) {
exit ('No file was added.');
} //www.jb51.net
$contents = implode("\r\n", $this->headers);
$contents .= "\r\n";
$contents .= "MIME-Version: 1.0\r\n";
$contents .= "Content-Type: multipart/related;\r\n";
$contents .= "\tboundary=\"{$this->boundary}\";\r\n";
$contents .= "\ttype=\"" . $this->files[0]['mimetype'] . "\"\r\n";
$contents .= "X-MimeOLE: Produced By Mht File Maker v1.0 beta\r\n";
$contents .= "\r\n";
$contents .= "This is a multi-part message in MIME format.\r\n";
$contents .= "\r\n";
foreach ($this->files as $file) {
$contents .= "--{$this->boundary}\r\n";
$contents .= "Content-Type: $file[mimetype]\r\n";
$contents .= "Content-Transfer-Encoding: $file[encoding]\r\n";
$contents .= "Content-Location: $file[filepath]\r\n";
$contents .= "\r\n";
$contents .= $file['filecont'];
$contents .= "\r\n";
}
$contents .= "--{$this->boundary}--\r\n";
return $contents;
}
function MakeFile($filename){
$contents = $this->GetFile();
$fp = fopen($filename, 'w');
fwrite($fp, $contents);
fclose($fp);
}
function GetMimeType($filename){
$pathinfo = pathinfo($filename);
switch ($pathinfo['extension']) {
case 'htm': $mimetype = 'text/html'; break;
case 'html': $mimetype = 'text/html'; break;
case 'txt': $mimetype = 'text/plain'; break;
case 'cgi': $mimetype = 'text/plain'; break;
case 'php': $mimetype = 'text/plain'; break;
case 'css': $mimetype = 'text/css'; break;
case 'jpg': $mimetype = 'image/jpeg'; break;
case 'jpeg': $mimetype = 'image/jpeg'; break;
case 'jpe': $mimetype = 'image/jpeg'; break;
case 'gif': $mimetype = 'image/gif'; break;
case 'png': $mimetype = 'image/png'; break;
default: $mimetype = 'application/octet-stream'; break;
}
return $mimetype;
}
}
step 4. Will deal with mht Save or download files
We have two ways to send files to the client :
One is to generate a doc file , And then put this doc Record the address of the document , Finally, let the client download this doc.
Another is direct transmission html request , modify HTML Agreed header part , Send the contents of the file directly to the client , You can also let the client download to this doc file .
for example : Download directly to the client :
$filename = $oUser->truename?$oUser->truename." Resume of ":" resume ";
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".strlen($content));
header('Content-Type: application/octet-stream');
$filename=iconv("utf-8","gb2312",$filename);
header('Content-Disposition: attachment; filename='.$filename.'.doc');
header("Pragma:no-cache");
header("Expires:0");
echo $content;
end
There are other ways , That's using PHPword Expand , Record again in the next article
边栏推荐
- laravel 时区问题timezone
- 解决msvcp120d.dll和msvcr120d.dll缺失
- libvirt 中体验容器
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- 网络通讯之Socket-Tcp(一)
- Kubernetes 三打探针及探针方式
- Xml的(DTD,xml解析,xml建模)
- Web security summary
- (数据库提权——Redis)Redis未授权访问漏洞总结
- Redis 笔记 01:入门篇
猜你喜欢
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
CGroup introduction
The excel table is transferred to word, and the table does not exceed the edge paper range
Qt+vtk+occt reading iges/step model
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
OPenGL 基本知识(根据自己理解整理)
Vulnhub's cereal
VS2015的下载地址和安装教程
网络通讯之Socket-Tcp(一)
Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022
随机推荐
Momentum of vulnhub
Simple factory and factory method mode
Slam mapping and autonomous navigation simulation based on turnlebot3
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
The world's most popular font editor FontCreator tool
Go语言实现静态服务器
cgroup简介
抓包整理外篇fiddler———— 会话栏与过滤器[二]
XML (DTD, XML parsing, XML modeling)
PHP导出word方法(一mht)
Kubernetes 三打探针及探针方式
Ripper of vulnhub
ArcGIS application (XXI) ArcMap method of deleting layer specified features
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
typeScript
Raven2 of vulnhub
vulnhub之presidential
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
【mysql专项】读锁和写锁
Vulnhub's presidential