mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-14 03:56:40 +00:00
Limited PrintArea Support for Html/Pdf
Fix #3941. Html and Pdf Writers will show PrintArea if specified rather than the entire sheet. Html Writer, which needs to handle both screen and print views, will use PrintArea through CSS; PDF, which needs to handle only print view, will use it by not including rows and columns which are not in the PrintArea. The support is limited because: - PrintArea can consist of several different ranges. The writers will use the PrintArea only if it consists of a single range. - Html use will be effective only if `useInlineCSS` is set to `false`, which is its default value. - Other aspects of page setup, e.g. horizontal centered, remain unsupported, with or without PrintArea. Html Writer will also set a `data-printarea` attribute on the `table` declaration for a worksheet when appropriate. Html Reader will recognize and process that attribute. This usage does not require that the PrintArea consist of a single range.
This commit is contained in:
@@ -533,6 +533,10 @@ class Html extends BaseReader
|
||||
$sheet->setShowGridlines(in_array('gridlines', $classes, true));
|
||||
$sheet->setPrintGridlines(in_array('gridlinesp', $classes, true));
|
||||
}
|
||||
if (isset($attributeArray['data-printarea'])) {
|
||||
$sheet->getPageSetup()
|
||||
->setPrintArea($attributeArray['data-printarea']);
|
||||
}
|
||||
if ('rtl' === ($attributeArray['dir'] ?? '')) {
|
||||
$sheet->setRightToLeft(true);
|
||||
}
|
||||
|
||||
@@ -511,6 +511,14 @@ class Html extends BaseWriter
|
||||
return [$cellType, $startTag, $endTag];
|
||||
}
|
||||
|
||||
private int $printAreaLowRow = -1;
|
||||
|
||||
private int $printAreaHighRow = -1;
|
||||
|
||||
private int $printAreaLowCol = -1;
|
||||
|
||||
private int $printAreaHighCol = -1;
|
||||
|
||||
/**
|
||||
* Generate sheet data.
|
||||
*/
|
||||
@@ -529,6 +537,17 @@ class Html extends BaseWriter
|
||||
$activeSheet = $this->spreadsheet->getActiveSheetIndex();
|
||||
|
||||
foreach ($sheets as $sheet) {
|
||||
$this->printAreaLowRow = -1;
|
||||
$this->printAreaHighRow = -1;
|
||||
$this->printAreaLowCol = -1;
|
||||
$this->printAreaHighCol = -1;
|
||||
$printArea = $sheet->getPageSetup()->getPrintArea();
|
||||
if (Preg::isMatch('/^([a-z]+)([0-9]+):([a-z]+)([0-9]+)$/i', $printArea, $matches)) {
|
||||
$this->printAreaLowCol = Coordinate::columnIndexFromString($matches[1]);
|
||||
$this->printAreaHighCol = Coordinate::columnIndexFromString($matches[3]);
|
||||
$this->printAreaLowRow = (int) $matches[2];
|
||||
$this->printAreaHighRow = (int) $matches[4];
|
||||
}
|
||||
// save active cells
|
||||
$selectedCells = $sheet->getSelectedCells();
|
||||
// Write table header
|
||||
@@ -1308,16 +1327,18 @@ class Html extends BaseWriter
|
||||
$float = $this->getFloat($worksheet);
|
||||
$prntgrid = $worksheet->getPrintGridlines();
|
||||
$viewgrid = $this->isPdf ? $prntgrid : $worksheet->getShowGridlines();
|
||||
$printArea = $worksheet->getPageSetup()->getPrintArea();
|
||||
$dataPrint = ($printArea === '') ? '' : (" data-printarea='" . htmlspecialchars($printArea) . "'");
|
||||
if ($viewgrid && $prntgrid) {
|
||||
$html = " <table border='1' cellpadding='1'$rtl $id cellspacing='1' style='$style' class='gridlines gridlinesp$float'>" . PHP_EOL;
|
||||
$html = " <table border='1' cellpadding='1'$rtl$dataPrint $id cellspacing='1' style='$style' class='gridlines gridlinesp$float'>" . PHP_EOL;
|
||||
} elseif ($viewgrid) {
|
||||
$html = " <table border='0' cellpadding='0'$rtl $id cellspacing='0' style='$style' class='gridlines$float'>" . PHP_EOL;
|
||||
$html = " <table border='0' cellpadding='0'$rtl$dataPrint $id cellspacing='0' style='$style' class='gridlines$float'>" . PHP_EOL;
|
||||
} elseif ($prntgrid) {
|
||||
$html = " <table border='0' cellpadding='0'$rtl $id cellspacing='0' style='$style' class='gridlinesp$float'>" . PHP_EOL;
|
||||
$html = " <table border='0' cellpadding='0'$rtl$dataPrint $id cellspacing='0' style='$style' class='gridlinesp$float'>" . PHP_EOL;
|
||||
} elseif ($float === '') {
|
||||
$html = " <table border='0' cellpadding='1'$rtl $id cellspacing='0' style='$style'>" . PHP_EOL;
|
||||
$html = " <table border='0' cellpadding='1'$rtl$dataPrint $id cellspacing='0' style='$style'>" . PHP_EOL;
|
||||
} else {
|
||||
$html = " <table border='0' cellpadding='1'$rtl $id cellspacing='0' style='$style' class='$float'>" . PHP_EOL;
|
||||
$html = " <table border='0' cellpadding='1'$rtl$dataPrint $id cellspacing='0' style='$style' class='$float'>" . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
@@ -1327,10 +1348,12 @@ class Html extends BaseWriter
|
||||
{
|
||||
if (!$this->useInlineCss) {
|
||||
$rtl = $this->getDir($worksheet);
|
||||
$printArea = $worksheet->getPageSetup()->getPrintArea();
|
||||
$dataPrint = ($printArea === '') ? '' : (" data-printarea='" . htmlspecialchars($printArea) . "'");
|
||||
$float = $this->getFloat($worksheet);
|
||||
$gridlines = $worksheet->getShowGridlines() ? ' gridlines' : '';
|
||||
$gridlinesp = $worksheet->getPrintGridlines() ? ' gridlinesp' : '';
|
||||
$html .= " <table border='0' cellpadding='0' cellspacing='0'$rtl $id class='sheet$sheetIndex$gridlines$gridlinesp$float'>" . PHP_EOL;
|
||||
$html .= " <table border='0' cellpadding='0' cellspacing='0'$rtl$dataPrint $id class='sheet$sheetIndex$gridlines$gridlinesp$float'>" . PHP_EOL;
|
||||
} else {
|
||||
$html .= $this->generateTableTagInline($worksheet, $id);
|
||||
}
|
||||
@@ -1797,7 +1820,7 @@ class Html extends BaseWriter
|
||||
$colNum = $key - 1;
|
||||
if (!$tcpdfInited && $key !== 1) {
|
||||
$tempspan = ($colNum > 1) ? " colspan='$colNum'" : '';
|
||||
$html .= "<td$tempspan></td>\n";
|
||||
$html .= "<td$tempspan></td>" . PHP_EOL;
|
||||
}
|
||||
$tcpdfInited = true;
|
||||
}
|
||||
@@ -1873,17 +1896,11 @@ class Html extends BaseWriter
|
||||
return $html;
|
||||
}
|
||||
|
||||
/** @param string[] $matches */
|
||||
private static function replaceNonAscii(array $matches): string
|
||||
{
|
||||
return '&#' . mb_ord($matches[0], 'UTF-8') . ';';
|
||||
}
|
||||
|
||||
private static function replaceControlChars(string $convert): string
|
||||
{
|
||||
return (string) preg_replace_callback(
|
||||
return Preg::replaceCallback(
|
||||
'/[\x00-\x1f]/',
|
||||
[self::class, 'replaceNonAscii'],
|
||||
fn (array $matches) => '&#' . ord($matches[0]) . ';',
|
||||
$convert
|
||||
);
|
||||
}
|
||||
@@ -2199,6 +2216,9 @@ class Html extends BaseWriter
|
||||
$htmlPage .= 'size: portrait; ';
|
||||
}
|
||||
$htmlPage .= '}' . PHP_EOL;
|
||||
if (!$this->isPdf) {
|
||||
$htmlPage .= $this->printAreaStyles($sheetId, $worksheet);
|
||||
}
|
||||
++$sheetId;
|
||||
}
|
||||
$htmlPage .= implode(PHP_EOL, [
|
||||
@@ -2223,8 +2243,44 @@ class Html extends BaseWriter
|
||||
return $htmlPage;
|
||||
}
|
||||
|
||||
private function printAreaStyles(int $sheetId, Worksheet $worksheet): string
|
||||
{
|
||||
$retVal = '';
|
||||
$printArea = $worksheet->getPageSetup()->getPrintArea();
|
||||
if (Preg::isMatch('/^([a-z]+)([0-9]+):([a-z]+)([0-9]+)$/i', $printArea, $matches)) {
|
||||
$lowCol = Coordinate::columnIndexFromString($matches[1]) - 1;
|
||||
$highCol = Coordinate::columnIndexFromString($matches[3]) - 1;
|
||||
$lowRow = (int) $matches[2] - 1;
|
||||
$highRow = (int) $matches[4] - 1;
|
||||
$retVal = '@media print {' . PHP_EOL;
|
||||
$highDataRow = $worksheet->getHighestDataRow();
|
||||
for ($row = 0; $row < $highDataRow; ++$row) {
|
||||
if ($row < $lowRow || $row > $highRow) {
|
||||
$retVal .= " table.sheet$sheetId tr.row$row td { display:none }" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
$highDataColumn = $worksheet->getHighestDataColumn();
|
||||
$highDataCol = Coordinate::columnIndexFromString($highDataColumn);
|
||||
for ($col = 0; $col < $highDataCol; ++$col) {
|
||||
if ($col < $lowCol || $col > $highCol) {
|
||||
$retVal .= " table.sheet$sheetId td.column$col { display:none }" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
$retVal .= '}' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $retVal;
|
||||
}
|
||||
|
||||
private function shouldGenerateRow(Worksheet $sheet, int $row): bool
|
||||
{
|
||||
if ($this->isPdf) {
|
||||
if ($this->printAreaLowRow >= 0) {
|
||||
if ($row < $this->printAreaLowRow || $row > $this->printAreaHighRow) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!($this instanceof Pdf\Mpdf || $this instanceof Pdf\Tcpdf)) {
|
||||
return true;
|
||||
}
|
||||
@@ -2234,6 +2290,14 @@ class Html extends BaseWriter
|
||||
|
||||
private function shouldGenerateColumn(Worksheet $sheet, string $colStr): bool
|
||||
{
|
||||
if ($this->isPdf) {
|
||||
if ($this->printAreaLowCol >= 0) {
|
||||
$col = Coordinate::columnIndexFromString($colStr);
|
||||
if ($col < $this->printAreaLowCol || $col > $this->printAreaHighCol) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!($this instanceof Pdf\Mpdf || $this instanceof Pdf\Tcpdf)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Dompdf;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf as DompdfWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PrintAreaTest extends TestCase
|
||||
{
|
||||
public function testPrintArea(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$inArray = [
|
||||
[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],
|
||||
];
|
||||
$sheet->fromArray($inArray);
|
||||
$sheet->getPageSetup()->setPrintArea('B2:D4');
|
||||
$writer = new DompdfWriter($spreadsheet);
|
||||
$html = $writer->generateHtmlAll();
|
||||
$html = preg_replace('/^ +/m', '', $html) ?? $html;
|
||||
$expectedArray = [
|
||||
'<tbody>',
|
||||
'<tr class="row1">',
|
||||
'<td class="column0 style0 n" style="width:42pt">7</td>',
|
||||
'<td class="column1 style0 n" style="width:42pt">8</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">9</td>',
|
||||
'</tr>',
|
||||
'<tr class="row2">',
|
||||
'<td class="column0 style0 n" style="width:42pt">12</td>',
|
||||
'<td class="column1 style0 n" style="width:42pt">13</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">14</td>',
|
||||
'</tr>',
|
||||
'<tr class="row3">',
|
||||
'<td class="column0 style0 n" style="width:42pt">17</td>',
|
||||
'<td class="column1 style0 n" style="width:42pt">18</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">19</td>',
|
||||
'</tr>',
|
||||
'</tbody>',
|
||||
];
|
||||
$expectedString = implode(PHP_EOL, $expectedArray);
|
||||
self::assertStringContainsString(
|
||||
$expectedString,
|
||||
$html
|
||||
);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Html as HtmlReader;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Html as HtmlWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PrintAreaTest extends TestCase
|
||||
{
|
||||
public function testPrintArea(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$inArray = [
|
||||
[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],
|
||||
];
|
||||
$sheet->fromArray($inArray);
|
||||
$sheet->getPageSetup()->setPrintArea('B2:D4');
|
||||
$writer = new HtmlWriter($spreadsheet);
|
||||
$html = $writer->generateHtmlAll();
|
||||
self::assertStringContainsString(
|
||||
"<table border='0' cellpadding='0' cellspacing='0' data-printarea='B2:D4' id='sheet0' class='sheet0 gridlines'>",
|
||||
$html
|
||||
);
|
||||
$expectedArray = [
|
||||
'@media print {',
|
||||
' table.sheet0 tr.row0 td { display:none }',
|
||||
' table.sheet0 tr.row4 td { display:none }',
|
||||
' table.sheet0 tr.row5 td { display:none }',
|
||||
' table.sheet0 td.column0 { display:none }',
|
||||
' table.sheet0 td.column4 { display:none }',
|
||||
'}',
|
||||
];
|
||||
$expectedString = implode(PHP_EOL, $expectedArray);
|
||||
self::assertStringContainsString(
|
||||
$expectedString,
|
||||
$html
|
||||
);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
||||
$reader = new HtmlReader();
|
||||
$spreadsheet2 = $reader->loadFromString($html);
|
||||
$sheet2 = $spreadsheet2->getActiveSheet();
|
||||
self::assertSame('B2:D4', $sheet2->getPageSetup()->getPrintArea());
|
||||
self::assertSame($inArray, $sheet2->toArray(null, false, false));
|
||||
$spreadsheet2->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Mpdf;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf as MpdfWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PrintAreaTest extends TestCase
|
||||
{
|
||||
public function testPrintArea(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$inArray = [
|
||||
[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],
|
||||
];
|
||||
$sheet->fromArray($inArray);
|
||||
$sheet->getPageSetup()->setPrintArea('B2:D4');
|
||||
$writer = new MpdfWriter($spreadsheet);
|
||||
$html = $writer->generateHtmlAll();
|
||||
$html = preg_replace('/^ +/m', '', $html) ?? $html;
|
||||
$expectedArray = [
|
||||
'<tbody>',
|
||||
'<tr class="row1">',
|
||||
'<td class="column1 style0 n" style="width:42pt">7</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">8</td>',
|
||||
'<td class="column3 style0 n" style="width:42pt">9</td>',
|
||||
'</tr>',
|
||||
'<tr class="row2">',
|
||||
'<td class="column1 style0 n" style="width:42pt">12</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">13</td>',
|
||||
'<td class="column3 style0 n" style="width:42pt">14</td>',
|
||||
'</tr>',
|
||||
'<tr class="row3">',
|
||||
'<td class="column1 style0 n" style="width:42pt">17</td>',
|
||||
'<td class="column2 style0 n" style="width:42pt">18</td>',
|
||||
'<td class="column3 style0 n" style="width:42pt">19</td>',
|
||||
'</tr>',
|
||||
'</tbody>',
|
||||
];
|
||||
$expectedString = implode(PHP_EOL, $expectedArray);
|
||||
self::assertStringContainsString(
|
||||
$expectedString,
|
||||
$html
|
||||
);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Writer\Tcpdf;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf as TcpdfWriter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PrintAreaTest extends TestCase
|
||||
{
|
||||
public function testPrintArea(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
$inArray = [
|
||||
[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],
|
||||
];
|
||||
$sheet->fromArray($inArray);
|
||||
$sheet->getPageSetup()->setPrintArea('B2:D4');
|
||||
$writer = new TcpdfWriter($spreadsheet);
|
||||
$html = $writer->generateHtmlAll();
|
||||
$html = preg_replace('/^ +/m', '', $html) ?? $html;
|
||||
$expectedArray = [
|
||||
'<tbody>',
|
||||
'<tr>',
|
||||
'<td></td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">7</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">8</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">9</td>',
|
||||
'</tr>',
|
||||
'<tr>',
|
||||
'<td></td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">12</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">13</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">14</td>',
|
||||
'</tr>',
|
||||
'<tr>',
|
||||
'<td></td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">17</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">18</td>',
|
||||
'<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt">19</td>',
|
||||
'</tr>',
|
||||
'</tbody>',
|
||||
];
|
||||
$expectedString = implode(PHP_EOL, $expectedArray);
|
||||
self::assertStringContainsString(
|
||||
$expectedString,
|
||||
$html
|
||||
);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user