@@ -511,6 +511,14 @@ private function generateSheetTags(int $row, int $theadStart, int $theadEnd, int
511511 return [$ cellType , $ startTag , $ endTag ];
512512 }
513513
514+ private int $ printAreaLowRow = -1 ;
515+
516+ private int $ printAreaHighRow = -1 ;
517+
518+ private int $ printAreaLowCol = -1 ;
519+
520+ private int $ printAreaHighCol = -1 ;
521+
514522 /**
515523 * Generate sheet data.
516524 */
@@ -529,6 +537,17 @@ public function generateSheetData(): string
529537 $ activeSheet = $ this ->spreadsheet ->getActiveSheetIndex ();
530538
531539 foreach ($ sheets as $ sheet ) {
540+ $ this ->printAreaLowRow = -1 ;
541+ $ this ->printAreaHighRow = -1 ;
542+ $ this ->printAreaLowCol = -1 ;
543+ $ this ->printAreaHighCol = -1 ;
544+ $ printArea = $ sheet ->getPageSetup ()->getPrintArea ();
545+ if (Preg::isMatch ('/^([a-z]+)([0-9]+):([a-z]+)([0-9]+)$/i ' , $ printArea , $ matches )) {
546+ $ this ->printAreaLowCol = Coordinate::columnIndexFromString ($ matches [1 ]);
547+ $ this ->printAreaHighCol = Coordinate::columnIndexFromString ($ matches [3 ]);
548+ $ this ->printAreaLowRow = (int ) $ matches [2 ];
549+ $ this ->printAreaHighRow = (int ) $ matches [4 ];
550+ }
532551 // save active cells
533552 $ selectedCells = $ sheet ->getSelectedCells ();
534553 // Write table header
@@ -1308,16 +1327,18 @@ private function generateTableTagInline(Worksheet $worksheet, string $id): strin
13081327 $ float = $ this ->getFloat ($ worksheet );
13091328 $ prntgrid = $ worksheet ->getPrintGridlines ();
13101329 $ viewgrid = $ this ->isPdf ? $ prntgrid : $ worksheet ->getShowGridlines ();
1330+ $ printArea = $ worksheet ->getPageSetup ()->getPrintArea ();
1331+ $ dataPrint = ($ printArea === '' ) ? '' : (" data-printarea=' " . htmlspecialchars ($ printArea ) . "' " );
13111332 if ($ viewgrid && $ prntgrid ) {
1312- $ html = " <table border='1' cellpadding='1' $ rtl $ id cellspacing='1' style=' $ style' class='gridlines gridlinesp $ float'> " . PHP_EOL ;
1333+ $ html = " <table border='1' cellpadding='1' $ rtl$ dataPrint $ id cellspacing='1' style=' $ style' class='gridlines gridlinesp $ float'> " . PHP_EOL ;
13131334 } elseif ($ viewgrid ) {
1314- $ html = " <table border='0' cellpadding='0' $ rtl $ id cellspacing='0' style=' $ style' class='gridlines $ float'> " . PHP_EOL ;
1335+ $ html = " <table border='0' cellpadding='0' $ rtl$ dataPrint $ id cellspacing='0' style=' $ style' class='gridlines $ float'> " . PHP_EOL ;
13151336 } elseif ($ prntgrid ) {
1316- $ html = " <table border='0' cellpadding='0' $ rtl $ id cellspacing='0' style=' $ style' class='gridlinesp $ float'> " . PHP_EOL ;
1337+ $ html = " <table border='0' cellpadding='0' $ rtl$ dataPrint $ id cellspacing='0' style=' $ style' class='gridlinesp $ float'> " . PHP_EOL ;
13171338 } elseif ($ float === '' ) {
1318- $ html = " <table border='0' cellpadding='1' $ rtl $ id cellspacing='0' style=' $ style'> " . PHP_EOL ;
1339+ $ html = " <table border='0' cellpadding='1' $ rtl$ dataPrint $ id cellspacing='0' style=' $ style'> " . PHP_EOL ;
13191340 } else {
1320- $ html = " <table border='0' cellpadding='1' $ rtl $ id cellspacing='0' style=' $ style' class=' $ float'> " . PHP_EOL ;
1341+ $ html = " <table border='0' cellpadding='1' $ rtl$ dataPrint $ id cellspacing='0' style=' $ style' class=' $ float'> " . PHP_EOL ;
13211342 }
13221343
13231344 return $ html ;
@@ -1327,10 +1348,12 @@ private function generateTableTag(Worksheet $worksheet, string $id, string &$htm
13271348 {
13281349 if (!$ this ->useInlineCss ) {
13291350 $ rtl = $ this ->getDir ($ worksheet );
1351+ $ printArea = $ worksheet ->getPageSetup ()->getPrintArea ();
1352+ $ dataPrint = ($ printArea === '' ) ? '' : (" data-printarea=' " . htmlspecialchars ($ printArea ) . "' " );
13301353 $ float = $ this ->getFloat ($ worksheet );
13311354 $ gridlines = $ worksheet ->getShowGridlines () ? ' gridlines ' : '' ;
13321355 $ gridlinesp = $ worksheet ->getPrintGridlines () ? ' gridlinesp ' : '' ;
1333- $ html .= " <table border='0' cellpadding='0' cellspacing='0' $ rtl $ id class='sheet $ sheetIndex$ gridlines$ gridlinesp$ float'> " . PHP_EOL ;
1356+ $ html .= " <table border='0' cellpadding='0' cellspacing='0' $ rtl$ dataPrint $ id class='sheet $ sheetIndex$ gridlines$ gridlinesp$ float'> " . PHP_EOL ;
13341357 } else {
13351358 $ html .= $ this ->generateTableTagInline ($ worksheet , $ id );
13361359 }
@@ -1797,7 +1820,7 @@ private function generateRow(Worksheet $worksheet, array $values, int $row, stri
17971820 $ colNum = $ key - 1 ;
17981821 if (!$ tcpdfInited && $ key !== 1 ) {
17991822 $ tempspan = ($ colNum > 1 ) ? " colspan=' $ colNum' " : '' ;
1800- $ html .= "<td $ tempspan></td> \n" ;
1823+ $ html .= "<td $ tempspan></td> " . PHP_EOL ;
18011824 }
18021825 $ tcpdfInited = true ;
18031826 }
@@ -1873,17 +1896,11 @@ private function generateRow(Worksheet $worksheet, array $values, int $row, stri
18731896 return $ html ;
18741897 }
18751898
1876- /** @param string[] $matches */
1877- private static function replaceNonAscii (array $ matches ): string
1878- {
1879- return '&# ' . mb_ord ($ matches [0 ], 'UTF-8 ' ) . '; ' ;
1880- }
1881-
18821899 private static function replaceControlChars (string $ convert ): string
18831900 {
1884- return ( string ) preg_replace_callback (
1901+ return Preg:: replaceCallback (
18851902 '/[\x00-\x1f]/ ' ,
1886- [ self ::class, ' replaceNonAscii ' ] ,
1903+ fn ( array $ matches ) => ' &# ' . ord ( $ matches [ 0 ]) . ' ; ' ,
18871904 $ convert
18881905 );
18891906 }
@@ -2199,6 +2216,9 @@ private function generatePageDeclarations(bool $generateSurroundingHTML): string
21992216 $ htmlPage .= 'size: portrait; ' ;
22002217 }
22012218 $ htmlPage .= '} ' . PHP_EOL ;
2219+ if (!$ this ->isPdf ) {
2220+ $ htmlPage .= $ this ->printAreaStyles ($ sheetId , $ worksheet );
2221+ }
22022222 ++$ sheetId ;
22032223 }
22042224 $ htmlPage .= implode (PHP_EOL , [
@@ -2223,8 +2243,44 @@ private function generatePageDeclarations(bool $generateSurroundingHTML): string
22232243 return $ htmlPage ;
22242244 }
22252245
2246+ private function printAreaStyles (int $ sheetId , Worksheet $ worksheet ): string
2247+ {
2248+ $ retVal = '' ;
2249+ $ printArea = $ worksheet ->getPageSetup ()->getPrintArea ();
2250+ if (Preg::isMatch ('/^([a-z]+)([0-9]+):([a-z]+)([0-9]+)$/i ' , $ printArea , $ matches )) {
2251+ $ lowCol = Coordinate::columnIndexFromString ($ matches [1 ]) - 1 ;
2252+ $ highCol = Coordinate::columnIndexFromString ($ matches [3 ]) - 1 ;
2253+ $ lowRow = (int ) $ matches [2 ] - 1 ;
2254+ $ highRow = (int ) $ matches [4 ] - 1 ;
2255+ $ retVal = '@media print { ' . PHP_EOL ;
2256+ $ highDataRow = $ worksheet ->getHighestDataRow ();
2257+ for ($ row = 0 ; $ row < $ highDataRow ; ++$ row ) {
2258+ if ($ row < $ lowRow || $ row > $ highRow ) {
2259+ $ retVal .= " table.sheet $ sheetId tr.row $ row td { display:none } " . PHP_EOL ;
2260+ }
2261+ }
2262+ $ highDataColumn = $ worksheet ->getHighestDataColumn ();
2263+ $ highDataCol = Coordinate::columnIndexFromString ($ highDataColumn );
2264+ for ($ col = 0 ; $ col < $ highDataCol ; ++$ col ) {
2265+ if ($ col < $ lowCol || $ col > $ highCol ) {
2266+ $ retVal .= " table.sheet $ sheetId td.column $ col { display:none } " . PHP_EOL ;
2267+ }
2268+ }
2269+ $ retVal .= '} ' . PHP_EOL ;
2270+ }
2271+
2272+ return $ retVal ;
2273+ }
2274+
22262275 private function shouldGenerateRow (Worksheet $ sheet , int $ row ): bool
22272276 {
2277+ if ($ this ->isPdf ) {
2278+ if ($ this ->printAreaLowRow >= 0 ) {
2279+ if ($ row < $ this ->printAreaLowRow || $ row > $ this ->printAreaHighRow ) {
2280+ return false ;
2281+ }
2282+ }
2283+ }
22282284 if (!($ this instanceof Pdf \Mpdf || $ this instanceof Pdf \Tcpdf)) {
22292285 return true ;
22302286 }
@@ -2234,6 +2290,14 @@ private function shouldGenerateRow(Worksheet $sheet, int $row): bool
22342290
22352291 private function shouldGenerateColumn (Worksheet $ sheet , string $ colStr ): bool
22362292 {
2293+ if ($ this ->isPdf ) {
2294+ if ($ this ->printAreaLowCol >= 0 ) {
2295+ $ col = Coordinate::columnIndexFromString ($ colStr );
2296+ if ($ col < $ this ->printAreaLowCol || $ col > $ this ->printAreaHighCol ) {
2297+ return false ;
2298+ }
2299+ }
2300+ }
22372301 if (!($ this instanceof Pdf \Mpdf || $ this instanceof Pdf \Tcpdf)) {
22382302 return true ;
22392303 }
0 commit comments