diff --git a/samples/Basic/25_In_memory_image.php b/samples/Basic/25_In_memory_image.php index af02c64abb..b8cd20e730 100644 --- a/samples/Basic/25_In_memory_image.php +++ b/samples/Basic/25_In_memory_image.php @@ -2,12 +2,18 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing; +use PhpOffice\PhpSpreadsheet\Writer\BaseWriter; require __DIR__ . '/../Header.php'; // Create new Spreadsheet object $helper->log('Create new Spreadsheet object'); $spreadsheet = new Spreadsheet(); +$sheet1 = $spreadsheet->getActiveSheet(); +$sheet1->setTitle('SheetWithData'); +$sheet1->getCell('G1')->setValue('X'); +$sheet1->getCell('E5')->setValue('Y'); +$sheet1->getCell('A8')->setValue('Z'); // Set document properties $helper->log('Set document properties'); @@ -38,8 +44,35 @@ $drawing->setRenderingFunction(MemoryDrawing::RENDERING_JPEG); $drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT); $drawing->setHeight(36); -$drawing->setWorksheet($spreadsheet->getActiveSheet()); +$drawing->setWorksheet($sheet1); +$drawing->setCoordinates('C5'); + +$helper->log('Create new sheet'); +$sheet2 = $spreadsheet->createSheet(); +$sheet2->setTitle('SheetWithoutData'); + +// Add a drawing to the new worksheet +$helper->log('Add a drawing to the new worksheet'); +$drawing = new MemoryDrawing(); +$drawing->setName('Sample image'); +$drawing->setDescription('Sample image'); +$drawing->setImageResource($gdImage); +$drawing->setRenderingFunction(MemoryDrawing::RENDERING_JPEG); +$drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT); +$drawing->setHeight(36); +$drawing->setWorksheet($sheet2); $drawing->setCoordinates('C5'); // Save -$helper->write($spreadsheet, __FILE__, ['Xlsx', 'Html']); +$helper->write( + $spreadsheet, + __FILE__, + ['Xlsx', 'Html'], + false, + function (BaseWriter $writer): void { + if (method_exists($writer, 'writeAllSheets')) { + $writer->writeAllSheets(); + } + } +); +$spreadsheet->disconnectWorksheets(); diff --git a/samples/Pdf/21c_Pdf.php b/samples/Pdf/21c_Pdf.php index 211f8be9a5..f556dcb2ff 100644 --- a/samples/Pdf/21c_Pdf.php +++ b/samples/Pdf/21c_Pdf.php @@ -2,15 +2,52 @@ use PhpOffice\PhpSpreadsheet\IOFactory; 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. + +function addHeadersFootersMpdf2000(string $html): string +{ + $pagerepl = << + +
+ My document header +
+
+ + + + + + + + +
My documentPage {PAGENO} of {nbpg}{DATE Y-m-j}
+
+ $simulatedBodyStart + + EOF; + + return preg_replace($bodystring, $bodyrepl, $html); +} + $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $counter = 0; $helper->log('Populate spreadsheet'); -for ($row = 1; $row < 501; ++$row) { +for ($row = 1; $row < 1001; ++$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)); @@ -21,4 +58,13 @@ $helper->log('Write to Mpdf'); IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class); $helper->write($spreadsheet, __FILE__, ['Pdf']); +$helper->write( + $spreadsheet, + __FILE__, + ['Pdf'], + false, + function (Mpdf $writer): void { + $writer->setEditHtmlCallback('addHeadersFootersMpdf2000'); + } +); $spreadsheet->disconnectWorksheets(); diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index f79cf1ae5e..53cd86beac 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -504,7 +504,8 @@ public function generateSheetData() $html .= $startTag; // Write row if there are HTML table cells in it - if (!isset($this->isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row])) { + $mpdfInvisible = $this->isMPdf && $sheet->getRowDimension($row)->getVisible() === false; + if (!$mpdfInvisible && !isset($this->isSpannedRow[$sheet->getParent()->getIndex($sheet)][$row])) { // Start a new rowData $rowData = []; // Loop through columns @@ -732,9 +733,14 @@ private function writeImageInCell(Worksheet $worksheet, $coordinates) // Because of the nature of tables, width is more important than height. // max-width: 100% ensures that image doesnt overflow containing cell + // However, PR #3535 broke test + // 25_In_memory_image, apparently because + // of the use of max-with. In addition, + // non-memory-drawings don't use max-width. + // Its use here is suspect and is being eliminated. // width: X sets width of supplied image. // As a result, images bigger than cell will be contained and images smaller will not get stretched - $html .= '' . $filedesc . ''; } } @@ -1498,10 +1504,6 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp // Sheet index $sheetIndex = $worksheet->getParentOrThrow()->getIndex($worksheet); $html = $this->generateRowStart($worksheet, $sheetIndex, $row); - $generateDiv = $this->isMPdf && $worksheet->getRowDimension($row + 1)->getVisible() === false; - if ($generateDiv) { - $html .= '
' . PHP_EOL; - } // Write cells $colNum = 0; @@ -1546,9 +1548,6 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp } // Write row end - if ($generateDiv) { - $html .= '
' . PHP_EOL; - } $html .= ' ' . PHP_EOL; // Return diff --git a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php index d0ce9ed458..d781483eb0 100644 --- a/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php +++ b/src/PhpSpreadsheet/Writer/Pdf/Mpdf.php @@ -3,11 +3,13 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Pdf; use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; -use PhpOffice\PhpSpreadsheet\Writer\Html; use PhpOffice\PhpSpreadsheet\Writer\Pdf; class Mpdf extends Pdf { + public const SIMULATED_BODY_START = ''; + private const BODY_TAG = ''; + /** @var bool */ protected $isMPdf = true; @@ -61,16 +63,22 @@ public function save($filename, int $flags = 0): void $pdf->SetCreator($this->spreadsheet->getProperties()->getCreator()); $html = $this->generateHTMLAll(); - $bodyLocation = strpos($html, Html::BODY_LINE); + $bodyLocation = strpos($html, self::SIMULATED_BODY_START); + if ($bodyLocation === false) { + $bodyLocation = strpos($html, self::BODY_TAG); + if ($bodyLocation !== false) { + $bodyLocation += strlen(self::BODY_TAG); + } + } // Make sure first data presented to Mpdf includes body tag + // (and any htmlpageheader/htmlpagefooter tags) // 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)); + foreach (explode("\n", $html) as $line) { + $pdf->WriteHTML("$line\n"); } // Write to file