@@ -158,6 +158,10 @@ class Html extends BaseWriter
158
158
159
159
private string $ getFalse = 'FALSE ' ;
160
160
161
+ protected bool $ rtlSheets = false ;
162
+
163
+ protected bool $ ltrSheets = false ;
164
+
161
165
/**
162
166
* Create a new HTML.
163
167
*/
@@ -186,11 +190,31 @@ public function save($filename, int $flags = 0): void
186
190
$ this ->maybeCloseFileHandle ();
187
191
}
188
192
193
+ protected function checkRtlAndLtr (): void
194
+ {
195
+ $ this ->rtlSheets = false ;
196
+ $ this ->ltrSheets = false ;
197
+ if ($ this ->sheetIndex === null ) {
198
+ foreach ($ this ->spreadsheet ->getAllSheets () as $ sheet ) {
199
+ if ($ sheet ->getRightToLeft ()) {
200
+ $ this ->rtlSheets = true ;
201
+ } else {
202
+ $ this ->ltrSheets = true ;
203
+ }
204
+ }
205
+ } else {
206
+ if ($ this ->spreadsheet ->getSheet ($ this ->sheetIndex )->getRightToLeft ()) {
207
+ $ this ->rtlSheets = true ;
208
+ }
209
+ }
210
+ }
211
+
189
212
/**
190
213
* Save Spreadsheet as html to variable.
191
214
*/
192
215
public function generateHtmlAll (): string
193
216
{
217
+ $ this ->checkRtlAndLtr ();
194
218
$ sheets = $ this ->generateSheetPrep ();
195
219
foreach ($ sheets as $ sheet ) {
196
220
$ sheet ->calculateArrays ($ this ->preCalculateFormulas );
@@ -369,7 +393,8 @@ public function generateHTMLHeader(bool $includeStyles = false): string
369
393
// Construct HTML
370
394
$ properties = $ this ->spreadsheet ->getProperties ();
371
395
$ html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> ' . PHP_EOL ;
372
- $ html .= '<html xmlns="http://www.w3.org/1999/xhtml"> ' . PHP_EOL ;
396
+ $ rtl = ($ this ->rtlSheets && !$ this ->ltrSheets ) ? " dir='rtl' " : '' ;
397
+ $ html .= '<html xmlns="http://www.w3.org/1999/xhtml" ' . $ rtl . '> ' . PHP_EOL ;
373
398
$ html .= ' <head> ' . PHP_EOL ;
374
399
$ html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ' . PHP_EOL ;
375
400
$ html .= ' <meta name="generator" content="PhpSpreadsheet, https://github.com/PHPOffice/PhpSpreadsheet" /> ' . PHP_EOL ;
@@ -1013,6 +1038,9 @@ public function buildCSS(bool $generateSurroundingHTML = true): array
1013
1038
// .s {}
1014
1039
$ css ['.s ' ]['text-align ' ] = 'left ' ; // STRING
1015
1040
1041
+ $ css ['.floatright ' ]['float ' ] = 'right ' ;
1042
+ $ css ['.floatleft ' ]['float ' ] = 'left ' ;
1043
+
1016
1044
// Calculate cell style hashes
1017
1045
foreach ($ this ->spreadsheet ->getCellXfCollection () as $ index => $ style ) {
1018
1046
$ css ['td.style ' . $ index . ', th.style ' . $ index ] = $ this ->createCSSStyle ($ style );
@@ -1221,21 +1249,52 @@ public function generateHTMLFooter(): string
1221
1249
return $ html ;
1222
1250
}
1223
1251
1252
+ private function getDir (Worksheet $ worksheet ): string
1253
+ {
1254
+ if ($ worksheet ->getRightToLeft ()) {
1255
+ return " dir='rtl' " ;
1256
+ }
1257
+ if ($ this ->rtlSheets ) {
1258
+ return " dir='ltr' " ;
1259
+ }
1260
+
1261
+ return '' ;
1262
+ }
1263
+
1264
+ private function getFloat (Worksheet $ worksheet ): string
1265
+ {
1266
+ $ float = '' ;
1267
+ if ($ worksheet ->getRightToLeft ()) {
1268
+ if ($ this ->ltrSheets ) {
1269
+ $ float = ' floatright ' ;
1270
+ }
1271
+ } else {
1272
+ if ($ this ->rtlSheets ) {
1273
+ $ float = ' floatleft ' ;
1274
+ }
1275
+ }
1276
+
1277
+ return $ float ;
1278
+ }
1279
+
1224
1280
private function generateTableTagInline (Worksheet $ worksheet , string $ id ): string
1225
1281
{
1226
1282
$ style = isset ($ this ->cssStyles ['table ' ])
1227
1283
? $ this ->assembleCSS ($ this ->cssStyles ['table ' ]) : '' ;
1228
-
1284
+ $ rtl = $ this ->getDir ($ worksheet );
1285
+ $ float = $ this ->getFloat ($ worksheet );
1229
1286
$ prntgrid = $ worksheet ->getPrintGridlines ();
1230
1287
$ viewgrid = $ this ->isPdf ? $ prntgrid : $ worksheet ->getShowGridlines ();
1231
1288
if ($ viewgrid && $ prntgrid ) {
1232
- $ html = " <table border='1' cellpadding='1' $ id cellspacing='1' style=' $ style' class='gridlines gridlinesp'> " . PHP_EOL ;
1289
+ $ html = " <table border='1' cellpadding='1' $ rtl $ id cellspacing='1' style=' $ style' class='gridlines gridlinesp $ float '> " . PHP_EOL ;
1233
1290
} elseif ($ viewgrid ) {
1234
- $ html = " <table border='0' cellpadding='0' $ id cellspacing='0' style=' $ style' class='gridlines'> " . PHP_EOL ;
1291
+ $ html = " <table border='0' cellpadding='0' $ rtl $ id cellspacing='0' style=' $ style' class='gridlines $ float '> " . PHP_EOL ;
1235
1292
} elseif ($ prntgrid ) {
1236
- $ html = " <table border='0' cellpadding='0' $ id cellspacing='0' style=' $ style' class='gridlinesp'> " . PHP_EOL ;
1293
+ $ html = " <table border='0' cellpadding='0' $ rtl $ id cellspacing='0' style=' $ style' class='gridlinesp $ float'> " . PHP_EOL ;
1294
+ } elseif ($ float === '' ) {
1295
+ $ html = " <table border='0' cellpadding='1' $ rtl $ id cellspacing='0' style=' $ style'> " . PHP_EOL ;
1237
1296
} else {
1238
- $ html = " <table border='0' cellpadding='1' $ id cellspacing='0' style=' $ style'> " . PHP_EOL ;
1297
+ $ html = " <table border='0' cellpadding='1' $ rtl $ id cellspacing='0' style=' $ style' class=' $ float '> " . PHP_EOL ;
1239
1298
}
1240
1299
1241
1300
return $ html ;
@@ -1244,9 +1303,11 @@ private function generateTableTagInline(Worksheet $worksheet, string $id): strin
1244
1303
private function generateTableTag (Worksheet $ worksheet , string $ id , string &$ html , int $ sheetIndex ): void
1245
1304
{
1246
1305
if (!$ this ->useInlineCss ) {
1306
+ $ rtl = $ this ->getDir ($ worksheet );
1307
+ $ float = $ this ->getFloat ($ worksheet );
1247
1308
$ gridlines = $ worksheet ->getShowGridlines () ? ' gridlines ' : '' ;
1248
1309
$ gridlinesp = $ worksheet ->getPrintGridlines () ? ' gridlinesp ' : '' ;
1249
- $ html .= " <table border='0' cellpadding='0' cellspacing='0' $ id class='sheet $ sheetIndex$ gridlines$ gridlinesp'> " . PHP_EOL ;
1310
+ $ html .= " <table border='0' cellpadding='0' cellspacing='0' $ rtl $ id class='sheet $ sheetIndex$ gridlines$ gridlinesp$ float '> " . PHP_EOL ;
1250
1311
} else {
1251
1312
$ html .= $ this ->generateTableTagInline ($ worksheet , $ id );
1252
1313
}
@@ -1265,10 +1326,12 @@ private function generateTableHeader(Worksheet $worksheet, bool $showid = true):
1265
1326
// Construct HTML
1266
1327
$ html = '' ;
1267
1328
$ id = $ showid ? "id='sheet $ sheetIndex' " : '' ;
1329
+ $ clear = ($ this ->rtlSheets && $ this ->ltrSheets ) ? '; clear:both ' : '' ;
1330
+
1268
1331
if ($ showid ) {
1269
- $ html .= "<div style='page: page $ sheetIndex'> " . PHP_EOL ;
1332
+ $ html .= "<div style='page: page $ sheetIndex$ clear '> " . PHP_EOL ;
1270
1333
} else {
1271
- $ html .= "<div style='page: page $ sheetIndex' class='scrpgbrk'> " . PHP_EOL ;
1334
+ $ html .= "<div style='page: page $ sheetIndex$ clear ' class='scrpgbrk'> " . PHP_EOL ;
1272
1335
}
1273
1336
1274
1337
$ this ->generateTableTag ($ worksheet , $ id , $ html , $ sheetIndex );
0 commit comments