PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.16k stars 3.37k forks source link

converting a .xlsx file to .pdf not retain the specified print area #3941

Open joerg-jw opened 4 months ago

joerg-jw commented 4 months ago

Description of the bug Why does converting a .xlsx file to .pdf not retain the specified print area and instead produces the entire page without selection?

mPDF version 8.2

PHP Version and environment (server type, cli provider etc., enclosing libraries and their respective versions) 8.2.12

Reproducible PHP+CSS+HTML snippet suffering by the error

`//<?php //https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-printer-options-for-excel-files require '../../vendor/autoload.php';

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

// Lade die vorhandene Excel-Datei $spreadsheet = IOFactory::createReader('Xlsx')->load('hello_world.xlsx');

// Arbeite mit der geladenen Excel-Datei $sheet = $spreadsheet->getActiveSheet();

// Ändere eine Zelle $sheet->setCellValue('B2', 'Updated Content');

// Beispiel: Setze den Druckbereich erneut (Beispiel: A1 bis D20) $sheet->getPageSetup()->setPrintArea('A1:J10'); $spreadsheet->getActiveSheet()->getPageSetup()->setPrintArea('A1:E5'); $spreadsheet->getActiveSheet()->getPageSetup()->setHorizontalCentered(true); $spreadsheet->getActiveSheet()->getPageSetup()->setVerticalCentered(true); //$spreadsheet->getActiveSheet()->getPageSetup()->setScale(100);

// Speichere die Änderungen in derselben Datei $excelWriter = IOFactory::createWriter($spreadsheet, 'Xlsx'); $excelWriter->save('hello_world_updated.xlsx');

// Lade die Excel-Datei $spreadsheetUpdated = IOFactory::load('hello_world_updated.xlsx');

// Setze den Druckbereich (angenommen, der Druckbereich ist A1:B10) $spreadsheetUpdated->getActiveSheet()->getPageSetup()->setPrintArea('A1:E5'); print_r($spreadsheetUpdated->getActiveSheet()->getPageSetup()->getPrintArea());

// Erstelle das PDF $pdfWriter = new Mpdf($spreadsheetUpdated); $pdfWriter->save('hello_world_updated.pdf'); ?>``

oleibman commented 4 months ago

We currently support printArea only for Xlsx and Xls. I will treat this issue as a feature request rather than a bug. I offhand have no idea how difficult it might be to implement - parsing the print area can be tricky (though not in your examples nor, I'm sure, in most of the applications that use it). Once that is done, we would need to integrate it with the Html Writer (the output of which is used by Pdf Writer). And, even when that is done, I'm not sure if we can satisfy the additional options (e.g. HorizontalCentered). At any rate, I'll put it on my to-do list, but I don't expect that I would get to it any time soon.