mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-18 20:00:46 +00:00
8a6142947e
* Minor Changes to Writer/Mpdf and Writer/Html Changed a sample to illustrate how to add header/footer in Mpdf using setHtmlEditCallback. This uses custom Html tags, and, like body, it appears that these must be defined in the first writeHtml. Adjust writeMpdf to permit this by using a new constant `SIMULATED_BODY_START`, defined as an Html comment, as a delimiter. Sample 21c_Pdf, to which the header/footer code is added, had been introduced with PR #2434 to ensure that the body tag was always in the first chunk. However, PR #3016 accidentally invalidated that test by reducing the number of style lines so that the sample now included the body tag in its first 1000 records rather than afterwards. This change puts it past record 1000 again. Inspecting the results of all the Html/Pdf samples after this change, it turns out that sample 25_In_memory_image was accidentally broken by PR #3535 - the combination of `max-width:100%` (already present before that change) with `position:absolute` (introduced with that change) made the memory drawing disappear from the rendered html when the image occurs in a column after the last column with data in it. It appears that there is no need for max-width (drawings which are not memory drawings do not use it), so it is dropped. The sample is changed to add a second page with a memory drawing, one page with the memory drawing after the last data column, and one with it before. The Html results now reflect the Xlsx result, as they should. * Minor Performance Improvements Anonymous function.
79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
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');
|
|
$spreadsheet->getProperties()->setCreator('Maarten Balliauw')
|
|
->setLastModifiedBy('Maarten Balliauw')
|
|
->setTitle('Office 2007 XLSX Test Document')
|
|
->setSubject('Office 2007 XLSX Test Document')
|
|
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
|
|
->setKeywords('office 2007 openxml php')
|
|
->setCategory('Test result file');
|
|
|
|
// Generate an image
|
|
$helper->log('Generate an image');
|
|
$gdImage = @imagecreatetruecolor(120, 20);
|
|
if (!$gdImage) {
|
|
die('Cannot Initialize new GD image stream');
|
|
}
|
|
|
|
$textColor = imagecolorallocate($gdImage, 255, 255, 255);
|
|
imagestring($gdImage, 1, 5, 5, 'Created with PhpSpreadsheet', $textColor);
|
|
|
|
// Add a drawing to the worksheet
|
|
$helper->log('Add a drawing to the 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($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'],
|
|
false,
|
|
function (BaseWriter $writer): void {
|
|
if (method_exists($writer, 'writeAllSheets')) {
|
|
$writer->writeAllSheets();
|
|
}
|
|
}
|
|
);
|
|
$spreadsheet->disconnectWorksheets();
|