Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joerg-jw opened this issue Mar 9, 2024 · 1 comment
Open

Comments

@joerg-jw
Copy link

joerg-jw commented Mar 9, 2024

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
Copy link
Collaborator

oleibman commented Mar 9, 2024

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.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Mar 12, 2024
Tcpdf issues warnings when processing hidden rows. Some time ago, Mpdf was having problems in the same situation; this was resolved by not writing out hidden rows in the html which Mpdf uses to generate its file. The same solution can be easily applied to Tcpdf.

Neither Tcpdf nor Mpdf handles hidden columns. Dompdf doesn't have a problem with either rows or columns. At any rate, the solution for Tcpdf/Mpdf is to not write out the data in the hidden columns. It is more difficult to implement this for columns than for rows, but this PR should do the trick.

Html writer generally uses `display:none` for the data in suppressed columns. This works, but is technically incorrect - the "approved" method is `visibility:collapse` on the col element. However, Firefox doesn't handle that correctly (open bug was filed over a decade ago), and, since all browsers seem to handle the existing implementation, it is left alone.

Among other considerations, this PR is a necessary precursor for supporting printArea in Html/Pdf should we decide to do that (issue PHPOffice#3941).

Writer/Html protected property `$isMPdf` is deprecated with this PR in favor of testing for `instanceof Mpdf`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants