-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExportToWord.inc.php
More file actions
180 lines (168 loc) · 6.11 KB
/
Copy pathExportToWord.inc.php
File metadata and controls
180 lines (168 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
//------------------------------------------------------------------------------------------------------------------------------------------------------
// Class for export from html to doc (MS Word)
// Created by info from https://habr.com/post/168977/
//------------------------------------------------------------------------------------------------------------------------------------------------------
class ExportToWord {
private static $mimeDocSeparator = 'doc_file_separator'; // any text for mime separation
private static $srcEncoding = 'UTF-8'; // source project encoding
private static $imgRef = '/img/'; // path to the images directory in 'ref' format
private static $imgDir = 'c:/myproj/img/'; // path to the images directory in absolute format
public static function htmlToDoc($srcHtml, $srcCss, $filePath = NULL, $filePathEncoding = 'UTF-8') {
// Converting from html to *.doc into the file $filePath
$entities = static::htmlEntities($srcHtml); // Get image data
$body = '';
$body .= static::headDoc($srcCss);
$body .= static::bodyDoc($entities['html']);
$body .= $entities['imagesData'];
$body .= static::fileList($entities);
$body .= '--'.static::$mimeDocSeparator.'--';
$body = mb_ereg_replace('\\t+', '', $body); // remove tabs
// Save to file
if($filePath) {
if($filePathEncoding != static::$srcEncoding) $filePath = iconv(static::$srcEncoding, $filePathEncoding, $filePath);
file_put_contents($filePath, $body);
}
return $body;
}
private static function headDoc($css) {
// *.doc header
$head = 'MIME-Version: 1.0
Content-Type: multipart/related; boundary="'.static::$mimeDocSeparator.'"
--'.static::$mimeDocSeparator.'
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="UTF-8"
<html xmlns:o=3D"urn:schemas-microsoft-com:office:office"
xmlns:w=3D"urn:schemas-microsoft-com:office:word"
xmlns=3D"http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3DContent-Type content=3D"text/html; charset=3DUTF-8">
<meta name=3DProgId content=3DWord.Document>
<meta name=3DGenerator content=3D"Microsoft Word 11">
<meta name=3DOriginator content=3D"Microsoft Word 11">
<link rel=3DFile-List href=3D"filelist.xml">
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:GrammarState>Clean</w:GrammarState>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState=3D"false" LatentStyleCount=3D"156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:"Tahoma";
mso-fareast-font-family:"Tahoma";}
@page Section1
{size:595.3pt 841.9pt;
margin:1.5cm 1.5cm 1.5cm 1.5cm;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"\041E\0431\044B\0447\043D\0430\044F \0442\0430\0431\043B\=0438\0446\0430";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Tahoma";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;
width:100%;
}
td.br1{
border:1px solid black;
}
'.$css.'
</style>
<![endif]-->
</head>';
return $head;
}
private static function htmlEntities($html) {
// Get images from html and receiving their data for embeding
$imagesData = '';
$imagesNames = '';
preg_match_all('/<img\s*(.*?)\s*src\s*=\s*"(.+?)"(.*?)>/u', $html, $matches);
$i = 0;
foreach($matches[0] as $imgTag) {
$filePath = mb_ereg_replace(static::$imgRef, static::$imgDir, $matches[2][$i]);
$imageExt = pathinfo($matches[2][$i])['extension'];
$imageName = 'images'.$i.'.'.$imageExt;
$imagesNames .= '<o:File HRef=3D"'.$imageName.'"/>';
$imageData = chunk_split(base64_encode(file_get_contents($filePath)));
$imagesData .= '
--'.static::$mimeDocSeparator.'
Content-Location: images/'.$imageName.'
Content-Transfer-Encoding: base64
Content-Type: image/'.$imageExt.'
'.$imageData.'
';
$imageDesc = '
<v:imagedata src="images/'.$imageName.'" o:href=""/>
</v:shape><![endif]--><![if !vml]><span style="mso-ignore:vglayout"><img border=3D0 src="images/'.$imageName.'"
alt=3DHaut v:shapes="_x0000_i1057" '.$matches[2][$i].' '.$matches[1][$i].' '.$matches[3][$i].'></span><![endif]>';
$html = mb_ereg_replace($imgTag, $imageDesc, $html);
$i++;
}
$html = preg_replace('/=/u', '=3D', $html);
return ['html' => $html, 'imagesNames' => $imagesNames, 'imagesData' => $imagesData];
}
private static function bodyDoc($body) {
// *.doc body
$body = '<body><div class=3D"Section1">'.$body.'</div>
</body>
</html>';
return $body;
}
private static function footerDoc() {
// *.doc footer
$footer = '</div>
</body>
</html>
';
return $footer;
}
private static function fileList($entities) {
// XML for embeding images
$fileList = '
--'.static::$mimeDocSeparator.'
Content-Location: filelist.xml
Content-Transfer-Encoding: quoted-printable
Content-Type: text/xml; charset="utf-8"
<xml xmlns:o=3D"urn:schemas-microsoft-com:office:office">
<o:MainFile HRef=3D"../doc.doc"/>
'.$entities['imagesNames'].'
<o:File HRef=3D"filelist.xml"/>
</xml>
';
return $fileList;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------