Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Writer/Dompdf/PrintAreaTest.php
T
oleibman ed00f8e14f 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.
2025-11-11 22:28:41 -08:00

57 lines
1.9 KiB
PHP

<?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();
}
}