Skip to content

Commit fadfb72

Browse files
authored
Minor Changes for Mpdf, Dompdf (#3002)
See discussion in #2999. Mpdf is not acknowledging the styling that we're using to hide table rows. I have opened an issue with them, but enclosing the cells in the hidden row inside a div with appropriate css does seem to be a workaround, and that can be incorporated into PhpSpreadsheet. It's kludgey, and it isn't even valid HTML, but ... Mpdf also doesn't like the addition of the ```file:///``` prefix when using local images from Windows (sample 21). Results are better when that prefix is not added. Dompdf seemed to have problems with sample 21 images on both Windows and Unix, with or without the file prefix. It does, however, support data urls for both, so is changed to embed images. It's still not perfect - the image seems truncated to the row height - but the results are better. I will continue to research, but may proceed as-is if I don't find anything better to do. Html Writer was producing a file with mixed line endings on Windows. This didn't cause any harm, but it seems a bit sloppy. It is changed to always use PHP_EOL as a line ending.
1 parent 5c13b17 commit fadfb72

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

samples/Pdf/21a_Pdf.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
$helper->log('Set orientation to landscape');
1313
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
1414
$spreadsheet->setActiveSheetIndex(0)->setPrintGridlines(true);
15+
// Issue 2299 - mpdf can't handle hide rows without kludge
16+
$spreadsheet->getActiveSheet()->getRowDimension(2)->setVisible(false);
1517

1618
function changeGridlines(string $html): string
1719
{

src/PhpSpreadsheet/Writer/Html.php

+33-25
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Html extends BaseWriter
5454
*
5555
* @var bool
5656
*/
57-
private $embedImages = false;
57+
protected $embedImages = false;
5858

5959
/**
6060
* Use inline CSS?
@@ -630,11 +630,12 @@ private function extendRowsForChartsAndImages(Worksheet $worksheet, int $row): s
630630
*
631631
* @return string
632632
*/
633-
public static function winFileToUrl($filename)
633+
public static function winFileToUrl($filename, bool $mpdf = false)
634634
{
635635
// Windows filename
636636
if (substr($filename, 1, 2) === ':\\') {
637-
$filename = 'file:///' . str_replace('\\', '/', $filename);
637+
$protocol = $mpdf ? '' : 'file:///';
638+
$filename = $protocol . str_replace('\\', '/', $filename);
638639
}
639640

640641
return $filename;
@@ -676,9 +677,9 @@ private function writeImageInCell(Worksheet $worksheet, $coordinates)
676677
$filename = htmlspecialchars($filename, Settings::htmlEntityFlags());
677678

678679
$html .= PHP_EOL;
679-
$imageData = self::winFileToUrl($filename);
680+
$imageData = self::winFileToUrl($filename, $this->isMPdf);
680681

681-
if (($this->embedImages && !$this->isPdf) || substr($imageData, 0, 6) === 'zip://') {
682+
if ($this->embedImages || substr($imageData, 0, 6) === 'zip://') {
682683
$picture = @file_get_contents($filename);
683684
if ($picture !== false) {
684685
$imageDetails = getimagesize($filename);
@@ -1160,9 +1161,9 @@ private function generateTableHeader(Worksheet $worksheet, $showid = true)
11601161
$html = '';
11611162
$id = $showid ? "id='sheet$sheetIndex'" : '';
11621163
if ($showid) {
1163-
$html .= "<div style='page: page$sheetIndex'>\n";
1164+
$html .= "<div style='page: page$sheetIndex'>" . PHP_EOL;
11641165
} else {
1165-
$html .= "<div style='page: page$sheetIndex' class='scrpgbrk'>\n";
1166+
$html .= "<div style='page: page$sheetIndex' class='scrpgbrk'>" . PHP_EOL;
11661167
}
11671168

11681169
$this->generateTableTag($worksheet, $id, $html, $sheetIndex);
@@ -1457,6 +1458,10 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
14571458
// Sheet index
14581459
$sheetIndex = $worksheet->getParent()->getIndex($worksheet);
14591460
$html = $this->generateRowStart($worksheet, $sheetIndex, $row);
1461+
$generateDiv = $this->isMPdf && $worksheet->getRowDimension($row + 1)->getVisible() === false;
1462+
if ($generateDiv) {
1463+
$html .= '<div style="visibility:hidden; display:none;">' . PHP_EOL;
1464+
}
14601465

14611466
// Write cells
14621467
$colNum = 0;
@@ -1504,6 +1509,9 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
15041509
}
15051510

15061511
// Write row end
1512+
if ($generateDiv) {
1513+
$html .= '</div>' . PHP_EOL;
1514+
}
15071515
$html .= ' </tr>' . PHP_EOL;
15081516

15091517
// Return
@@ -1834,26 +1842,26 @@ private function generatePageDeclarations($generateSurroundingHTML)
18341842
} elseif ($orientation === \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT) {
18351843
$htmlPage .= 'size: portrait; ';
18361844
}
1837-
$htmlPage .= "}\n";
1845+
$htmlPage .= '}' . PHP_EOL;
18381846
++$sheetId;
18391847
}
1840-
$htmlPage .= <<<EOF
1841-
.navigation {page-break-after: always;}
1842-
.scrpgbrk, div + div {page-break-before: always;}
1843-
@media screen {
1844-
.gridlines td {border: 1px solid black;}
1845-
.gridlines th {border: 1px solid black;}
1846-
body>div {margin-top: 5px;}
1847-
body>div:first-child {margin-top: 0;}
1848-
.scrpgbrk {margin-top: 1px;}
1849-
}
1850-
@media print {
1851-
.gridlinesp td {border: 1px solid black;}
1852-
.gridlinesp th {border: 1px solid black;}
1853-
.navigation {display: none;}
1854-
}
1855-
1856-
EOF;
1848+
$htmlPage .= implode(PHP_EOL, [
1849+
'.navigation {page-break-after: always;}',
1850+
'.scrpgbrk, div + div {page-break-before: always;}',
1851+
'@media screen {',
1852+
' .gridlines td {border: 1px solid black;}',
1853+
' .gridlines th {border: 1px solid black;}',
1854+
' body>div {margin-top: 5px;}',
1855+
' body>div:first-child {margin-top: 0;}',
1856+
' .scrpgbrk {margin-top: 1px;}',
1857+
'}',
1858+
'@media print {',
1859+
' .gridlinesp td {border: 1px solid black;}',
1860+
' .gridlinesp th {border: 1px solid black;}',
1861+
' .navigation {display: none;}',
1862+
'}',
1863+
'',
1864+
]);
18571865
$htmlPage .= $generateSurroundingHTML ? ('</style>' . PHP_EOL) : '';
18581866

18591867
return $htmlPage;

src/PhpSpreadsheet/Writer/Pdf/Dompdf.php

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77

88
class Dompdf extends Pdf
99
{
10+
/**
11+
* embed images, or link to images.
12+
*
13+
* @var bool
14+
*/
15+
protected $embedImages = true;
16+
1017
/**
1118
* Gets the implementation of external PDF library that should be used.
1219
*

0 commit comments

Comments
 (0)