Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Writer/Html/ImageCopyTest.php
oleibman a32861a0a0 Copy from Xls(x) to Html/Pdf Loses Drawings (#2788)
Drawings in an Xlsx file are stored in such a way that Php can read their contents using the `zip:` protocol. This does not, however, work when the file is read by PhpSpreadsheet and then saved as Html or Pdf, since the browser will not recognize that protocol even if the file is available. Such drawings need to be saved in the html as embedded images in order for the copy to display them properly. This is true even when the writer is set to not embed images (default).

An additional problem arises when an Html file with an embedded image is read, because `Worksheet\Drawing::setPath` attempts to validate the path, which it cannot do for the `data:image` Url which embedded images use.

And yet another problem. Writer/Html writes out a MemoryDrawing as a png using the imagepng function; but then declares it as jpeg in the Html. This is now corrected.

And a fourth problem. Writer/Html ignores the last row if it contains nothing but a Memory Drawing, which can be true when copying an Xls file.

These changes are testable (it's how I discovered the second part of this parlay). I think it is also useful to add a sample to see the results of this type of copy.
2022-05-07 08:10:24 -07:00

71 lines
2.5 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Reader\Xls as XlsReader;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PhpOffice\PhpSpreadsheetTests\Functional;
class ImageCopyTest extends Functional\AbstractFunctional
{
/** @var string */
private $xlsxFile = '';
protected function tearDown(): void
{
if ($this->xlsxFile !== '') {
unlink($this->xlsxFile);
$this->xlsxFile = '';
}
}
public function testImageCopyXls(): void
{
$file = 'samples/templates/27template.xls';
$reader = new XlsReader();
$reloadedSpreadsheet = $reader->load($file);
$writer = new Html($reloadedSpreadsheet);
$writer->writeAllSheets();
self::assertFalse($writer->getEmbedImages());
$html = $writer->generateHTMLAll();
self::assertSame(4, substr_count($html, '<img'));
self::assertSame(0, substr_count($html, 'zip://'));
// all 4 images converted to png
self::assertSame(4, substr_count($html, 'data:image/png;base64'));
$this->writeAndReload($reloadedSpreadsheet, 'Html');
$reloadedSpreadsheet->disconnectWorksheets();
}
public function testImageCopyXlsx(): void
{
$file = 'samples/templates/27template.xls';
$reader = new XlsReader();
$spreadsheet = $reader->load($file);
$this->xlsxFile = File::temporaryFilename();
$writer = new XlsxWriter($spreadsheet);
$writer->save($this->xlsxFile);
$spreadsheet->disconnectWorksheets();
$reader2 = new XlsxReader();
$reloadedSpreadsheet = $reader2->load($this->xlsxFile);
$writer = new Html($reloadedSpreadsheet);
$writer->writeAllSheets();
self::assertFalse($writer->getEmbedImages());
$html = $writer->generateHTMLAll();
self::assertSame(4, substr_count($html, '<img'));
self::assertSame(0, substr_count($html, 'zip://'));
// "gif" is actually stored as png in this file
self::assertSame(2, substr_count($html, 'data:image/png;base64'));
//self::assertSame(1, substr_count($html, 'data:image/gif;base64'));
self::assertSame(2, substr_count($html, 'data:image/jpeg;base64'));
$this->writeAndReload($reloadedSpreadsheet, 'Html');
$reloadedSpreadsheet->disconnectWorksheets();
}
}