mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-01 09:27:49 +00:00
4a2b731ec2
Fix #4129. Fix #4168. Html Writer, which all the Pdf writers use, defines its charts and drawings (henceforth I will just use charts for this discussion) using position:absolute and z-index. Browsers handle this correctly, but none of the Pdf writers do, and I can't think of an alternative method of styling them. The result is that the charts take up too much or too little room on the Pdf. I suggested in the two discussions that treating the areas covered by the charts as merged cells might mitigate the problem. I think there are too many unknowns to do so automatically (and see next paragraph). However, adding to Spreadsheet new methods `mergeChartCellsForPdf` and `mergeDrawingCellsForPdf` allows the end user to do this if desired. The new methods are exercised for charts in samples/Chart/32_Chart_read_write_PDF, the results of which are much improved as a result. New samples/Pdf/21f_Drawing_mpdf does likewise for drawings. The new methods alter the spreadsheet they are working on, which could be a problem if you still wish to work with the spreadsheet after writing it to Pdf. In that case, making a copy of the spreadsheet, then calling the new methods on the copy, and writing the copy to Pdf is probably best.
51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
require_once __DIR__ . '/Mpdf2.php';
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
$sheet->getCell('A1')->setValue('A1');
|
|
$sheet->getCell('B1')->setValue('B');
|
|
$sheet->getCell('C1')->setValue('C');
|
|
$sheet->getCell('D1')->setValue('D');
|
|
$sheet->getCell('E1')->setValue('E');
|
|
$sheet->getCell('F1')->setValue('F');
|
|
$sheet->getCell('G1')->setValue('G');
|
|
$sheet->getCell('A2')->setValue('A2');
|
|
$sheet->getCell('A3')->setValue('A3');
|
|
$sheet->getCell('A4')->setValue('A4');
|
|
$sheet->getCell('A5')->setValue('A5');
|
|
$sheet->getCell('A6')->setValue('A6');
|
|
$sheet->getCell('A7')->setValue('A7');
|
|
$sheet->getCell('A8')->setValue('A8');
|
|
|
|
$helper->log('Add drawing to worksheet');
|
|
$drawing = new Drawing();
|
|
$drawing->setName('Blue Square');
|
|
$path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'images/blue_square.png';
|
|
$drawing->setPath($path);
|
|
$drawing->setResizeProportional(false);
|
|
$drawing->setWidth(320);
|
|
$drawing->setCoordinates('B2');
|
|
$drawing->setCoordinates2('G6');
|
|
$drawing->setWorksheet($sheet, true);
|
|
|
|
$helper->log('Merge drawing cells for Pdf');
|
|
$spreadsheet->mergeDrawingCellsForPdf();
|
|
|
|
$helper->log('Write to Mpdf');
|
|
$writer = new Mpdf($spreadsheet);
|
|
$filename = $helper->getFileName(__FILE__, 'pdf');
|
|
$writer->save($filename);
|
|
$helper->log("Saved $filename");
|
|
if (PHP_SAPI !== 'cli') {
|
|
echo '<a href="/download.php?type=pdf&name=' . basename($filename) . '">Download ' . basename($filename) . '</a><br />';
|
|
}
|
|
$spreadsheet->disconnectWorksheets();
|