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.
This commit is contained in:
oleibman 2022-08-13 18:28:22 -07:00 committed by GitHub
parent 5c13b179a1
commit fadfb727bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 25 deletions

View File

@ -12,6 +12,8 @@ $spreadsheet->getActiveSheet()->setShowGridLines(false);
$helper->log('Set orientation to landscape'); $helper->log('Set orientation to landscape');
$spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE); $spreadsheet->getActiveSheet()->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
$spreadsheet->setActiveSheetIndex(0)->setPrintGridlines(true); $spreadsheet->setActiveSheetIndex(0)->setPrintGridlines(true);
// Issue 2299 - mpdf can't handle hide rows without kludge
$spreadsheet->getActiveSheet()->getRowDimension(2)->setVisible(false);
function changeGridlines(string $html): string function changeGridlines(string $html): string
{ {

View File

@ -54,7 +54,7 @@ class Html extends BaseWriter
* *
* @var bool * @var bool
*/ */
private $embedImages = false; protected $embedImages = false;
/** /**
* Use inline CSS? * Use inline CSS?
@ -630,11 +630,12 @@ class Html extends BaseWriter
* *
* @return string * @return string
*/ */
public static function winFileToUrl($filename) public static function winFileToUrl($filename, bool $mpdf = false)
{ {
// Windows filename // Windows filename
if (substr($filename, 1, 2) === ':\\') { if (substr($filename, 1, 2) === ':\\') {
$filename = 'file:///' . str_replace('\\', '/', $filename); $protocol = $mpdf ? '' : 'file:///';
$filename = $protocol . str_replace('\\', '/', $filename);
} }
return $filename; return $filename;
@ -676,9 +677,9 @@ class Html extends BaseWriter
$filename = htmlspecialchars($filename, Settings::htmlEntityFlags()); $filename = htmlspecialchars($filename, Settings::htmlEntityFlags());
$html .= PHP_EOL; $html .= PHP_EOL;
$imageData = self::winFileToUrl($filename); $imageData = self::winFileToUrl($filename, $this->isMPdf);
if (($this->embedImages && !$this->isPdf) || substr($imageData, 0, 6) === 'zip://') { if ($this->embedImages || substr($imageData, 0, 6) === 'zip://') {
$picture = @file_get_contents($filename); $picture = @file_get_contents($filename);
if ($picture !== false) { if ($picture !== false) {
$imageDetails = getimagesize($filename); $imageDetails = getimagesize($filename);
@ -1160,9 +1161,9 @@ class Html extends BaseWriter
$html = ''; $html = '';
$id = $showid ? "id='sheet$sheetIndex'" : ''; $id = $showid ? "id='sheet$sheetIndex'" : '';
if ($showid) { if ($showid) {
$html .= "<div style='page: page$sheetIndex'>\n"; $html .= "<div style='page: page$sheetIndex'>" . PHP_EOL;
} else { } else {
$html .= "<div style='page: page$sheetIndex' class='scrpgbrk'>\n"; $html .= "<div style='page: page$sheetIndex' class='scrpgbrk'>" . PHP_EOL;
} }
$this->generateTableTag($worksheet, $id, $html, $sheetIndex); $this->generateTableTag($worksheet, $id, $html, $sheetIndex);
@ -1457,6 +1458,10 @@ class Html extends BaseWriter
// Sheet index // Sheet index
$sheetIndex = $worksheet->getParent()->getIndex($worksheet); $sheetIndex = $worksheet->getParent()->getIndex($worksheet);
$html = $this->generateRowStart($worksheet, $sheetIndex, $row); $html = $this->generateRowStart($worksheet, $sheetIndex, $row);
$generateDiv = $this->isMPdf && $worksheet->getRowDimension($row + 1)->getVisible() === false;
if ($generateDiv) {
$html .= '<div style="visibility:hidden; display:none;">' . PHP_EOL;
}
// Write cells // Write cells
$colNum = 0; $colNum = 0;
@ -1504,6 +1509,9 @@ class Html extends BaseWriter
} }
// Write row end // Write row end
if ($generateDiv) {
$html .= '</div>' . PHP_EOL;
}
$html .= ' </tr>' . PHP_EOL; $html .= ' </tr>' . PHP_EOL;
// Return // Return
@ -1834,26 +1842,26 @@ class Html extends BaseWriter
} elseif ($orientation === \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT) { } elseif ($orientation === \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_PORTRAIT) {
$htmlPage .= 'size: portrait; '; $htmlPage .= 'size: portrait; ';
} }
$htmlPage .= "}\n"; $htmlPage .= '}' . PHP_EOL;
++$sheetId; ++$sheetId;
} }
$htmlPage .= <<<EOF $htmlPage .= implode(PHP_EOL, [
.navigation {page-break-after: always;} '.navigation {page-break-after: always;}',
.scrpgbrk, div + div {page-break-before: always;} '.scrpgbrk, div + div {page-break-before: always;}',
@media screen { '@media screen {',
.gridlines td {border: 1px solid black;} ' .gridlines td {border: 1px solid black;}',
.gridlines th {border: 1px solid black;} ' .gridlines th {border: 1px solid black;}',
body>div {margin-top: 5px;} ' body>div {margin-top: 5px;}',
body>div:first-child {margin-top: 0;} ' body>div:first-child {margin-top: 0;}',
.scrpgbrk {margin-top: 1px;} ' .scrpgbrk {margin-top: 1px;}',
} '}',
@media print { '@media print {',
.gridlinesp td {border: 1px solid black;} ' .gridlinesp td {border: 1px solid black;}',
.gridlinesp th {border: 1px solid black;} ' .gridlinesp th {border: 1px solid black;}',
.navigation {display: none;} ' .navigation {display: none;}',
} '}',
'',
EOF; ]);
$htmlPage .= $generateSurroundingHTML ? ('</style>' . PHP_EOL) : ''; $htmlPage .= $generateSurroundingHTML ? ('</style>' . PHP_EOL) : '';
return $htmlPage; return $htmlPage;

View File

@ -7,6 +7,13 @@ use PhpOffice\PhpSpreadsheet\Writer\Pdf;
class Dompdf extends Pdf class Dompdf extends Pdf
{ {
/**
* embed images, or link to images.
*
* @var bool
*/
protected $embedImages = true;
/** /**
* Gets the implementation of external PDF library that should be used. * Gets the implementation of external PDF library that should be used.
* *