Skip to content

Mpdf With Very Many Styles #2434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions samples/Pdf/21c_Pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;

require __DIR__ . '/../Header.php';

// Issue 2432 - styles were too large to fit in first Mpdf chunk, causing problems.
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$counter = 0;
$helper->log('Populate spreadsheet');
for ($row = 1; $row < 501; ++$row) {
$sheet->getCell("A$row")->setValue(++$counter);
// Add many styles by using slight variations of font color for each.
$sheet->getCell("A$row")->getStyle()->getFont()->getColor()->setRgb(sprintf('%06x', $counter));
$sheet->getCell("B$row")->setValue(++$counter);
$sheet->getCell("C$row")->setValue(++$counter);
}

$helper->log('Write to Mpdf');
$writer = new Mpdf($spreadsheet);
$filename = $helper->getFileName('21c_Pdf_mpdf.xlsx', 'pdf');
$writer->save($filename);
$helper->log("Saved $filename");
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ private static function generateMeta($val, $desc)
: '';
}

public const BODY_LINE = ' <body>' . PHP_EOL;

/**
* Generate HTML header.
*
Expand Down Expand Up @@ -384,7 +386,7 @@ public function generateHTMLHeader($includeStyles = false)

$html .= ' </head>' . PHP_EOL;
$html .= '' . PHP_EOL;
$html .= ' <body>' . PHP_EOL;
$html .= self::BODY_LINE;

return $html;
}
Expand Down
9 changes: 9 additions & 0 deletions src/PhpSpreadsheet/Writer/Pdf/Mpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Writer\Pdf;

use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheet\Writer\Pdf;

class Mpdf extends Pdf
Expand Down Expand Up @@ -57,6 +58,14 @@ public function save($filename, int $flags = 0): void
$pdf->SetCreator($this->spreadsheet->getProperties()->getCreator());

$html = $this->generateHTMLAll();
$bodyLocation = strpos($html, Html::BODY_LINE);
// Make sure first data presented to Mpdf includes body tag
// so that Mpdf doesn't parse it as content. Issue 2432.
if ($bodyLocation !== false) {
$bodyLocation += strlen(Html::BODY_LINE);
$pdf->WriteHTML(substr($html, 0, $bodyLocation));
$html = substr($html, $bodyLocation);
}
foreach (\array_chunk(\explode(PHP_EOL, $html), 1000) as $lines) {
$pdf->WriteHTML(\implode(PHP_EOL, $lines));
}
Expand Down