@@ -158,6 +158,10 @@ class Html extends BaseWriter
158158
159159 private string $ getFalse = 'FALSE ' ;
160160
161+ protected bool $ rtlSheets = false ;
162+
163+ protected bool $ ltrSheets = false ;
164+
161165 /**
162166 * Create a new HTML.
163167 */
@@ -186,11 +190,31 @@ public function save($filename, int $flags = 0): void
186190 $ this ->maybeCloseFileHandle ();
187191 }
188192
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+
189212 /**
190213 * Save Spreadsheet as html to variable.
191214 */
192215 public function generateHtmlAll (): string
193216 {
217+ $ this ->checkRtlAndLtr ();
194218 $ sheets = $ this ->generateSheetPrep ();
195219 foreach ($ sheets as $ sheet ) {
196220 $ sheet ->calculateArrays ($ this ->preCalculateFormulas );
@@ -369,7 +393,8 @@ public function generateHTMLHeader(bool $includeStyles = false): string
369393 // Construct HTML
370394 $ properties = $ this ->spreadsheet ->getProperties ();
371395 $ 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 ;
373398 $ html .= ' <head> ' . PHP_EOL ;
374399 $ html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ' . PHP_EOL ;
375400 $ html .= ' <meta name="generator" content="PhpSpreadsheet, https://github.com/PHPOffice/PhpSpreadsheet" /> ' . PHP_EOL ;
@@ -1013,6 +1038,9 @@ public function buildCSS(bool $generateSurroundingHTML = true): array
10131038 // .s {}
10141039 $ css ['.s ' ]['text-align ' ] = 'left ' ; // STRING
10151040
1041+ $ css ['.floatright ' ]['float ' ] = 'right ' ;
1042+ $ css ['.floatleft ' ]['float ' ] = 'left ' ;
1043+
10161044 // Calculate cell style hashes
10171045 foreach ($ this ->spreadsheet ->getCellXfCollection () as $ index => $ style ) {
10181046 $ css ['td.style ' . $ index . ', th.style ' . $ index ] = $ this ->createCSSStyle ($ style );
@@ -1221,21 +1249,52 @@ public function generateHTMLFooter(): string
12211249 return $ html ;
12221250 }
12231251
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+
12241280 private function generateTableTagInline (Worksheet $ worksheet , string $ id ): string
12251281 {
12261282 $ style = isset ($ this ->cssStyles ['table ' ])
12271283 ? $ this ->assembleCSS ($ this ->cssStyles ['table ' ]) : '' ;
1228-
1284+ $ rtl = $ this ->getDir ($ worksheet );
1285+ $ float = $ this ->getFloat ($ worksheet );
12291286 $ prntgrid = $ worksheet ->getPrintGridlines ();
12301287 $ viewgrid = $ this ->isPdf ? $ prntgrid : $ worksheet ->getShowGridlines ();
12311288 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 ;
12331290 } 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 ;
12351292 } 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 ;
12371296 } 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 ;
12391298 }
12401299
12411300 return $ html ;
@@ -1244,9 +1303,11 @@ private function generateTableTagInline(Worksheet $worksheet, string $id): strin
12441303 private function generateTableTag (Worksheet $ worksheet , string $ id , string &$ html , int $ sheetIndex ): void
12451304 {
12461305 if (!$ this ->useInlineCss ) {
1306+ $ rtl = $ this ->getDir ($ worksheet );
1307+ $ float = $ this ->getFloat ($ worksheet );
12471308 $ gridlines = $ worksheet ->getShowGridlines () ? ' gridlines ' : '' ;
12481309 $ 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 ;
12501311 } else {
12511312 $ html .= $ this ->generateTableTagInline ($ worksheet , $ id );
12521313 }
@@ -1265,10 +1326,12 @@ private function generateTableHeader(Worksheet $worksheet, bool $showid = true):
12651326 // Construct HTML
12661327 $ html = '' ;
12671328 $ id = $ showid ? "id='sheet $ sheetIndex' " : '' ;
1329+ $ clear = ($ this ->rtlSheets && $ this ->ltrSheets ) ? '; clear:both ' : '' ;
1330+
12681331 if ($ showid ) {
1269- $ html .= "<div style='page: page $ sheetIndex'> " . PHP_EOL ;
1332+ $ html .= "<div style='page: page $ sheetIndex$ clear '> " . PHP_EOL ;
12701333 } else {
1271- $ html .= "<div style='page: page $ sheetIndex' class='scrpgbrk'> " . PHP_EOL ;
1334+ $ html .= "<div style='page: page $ sheetIndex$ clear ' class='scrpgbrk'> " . PHP_EOL ;
12721335 }
12731336
12741337 $ this ->generateTableTag ($ worksheet , $ id , $ html , $ sheetIndex );
0 commit comments