PHPOffice / PhpSpreadsheet

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

PDF multiple sheets #2236

Closed ismailiiui closed 1 year ago

ismailiiui commented 3 years ago

I have implemented this library for a client and we are in need of generating multiple page PDF by number of sheets in a spreadsheet. I can see it is not implemented at this point. I can however, for the time being, do some work around for this.

oleibman commented 3 years ago

Does the following not work for you?

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->writeAllSheets();
$writer->save($filename);
ismailiiui commented 3 years ago

Let me check that

ismailiiui commented 3 years ago

Ok I just checked, it does work but not sure why exactly it is creating 3-4 extra blank sheets along with it? For my work around, I created a single page spreadsheets with multiple page breaks to achieve the same result and it creates only the required number of sheets.

power725 commented 2 years ago

Thanks, Oleibman, That works for me. But I faced some issues. I would like to set header and footer on every page using mpdf but I can't find the way how to deal with it. So I would like you to help me as soon as possible.

Thanks, Vitaliy

oleibman commented 2 years ago

You might create a new class which extends PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf, and add the Mpdf header commands to the save method. If that doesn't work, this seems more appropriate for a new feature request rather than tacking it onto a mostly unrelated existing issue.

oleibman commented 2 years ago

I could not get the "native" Mpdf header commands to work to my satisfaction. However, the following seems to work (but may have memory problems with larger files):

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$counter = 0;
for ($row = 1; $row < 501; ++$row) {
    $sheet->getCell("A$row")->setValue(++$counter);
    $sheet->getCell("B$row")->setValue(++$counter);
    $sheet->getCell("C$row")->setValue(++$counter);
}

function addHeadersFooters(string $html): string
{
    $pagerepl = <<<EOF
@page page0 {
odd-header-name: html_myHeader1;
even-header-name: html_myHeader1;
odd-footer-name: html_myFooter2;
even-footer-name: html_myFooter2;

EOF;
    $html = preg_replace('/@page page0 {/', $pagerepl, $html);
    $bodystring = '/<body>/';
    $bodyrepl = <<<EOF
<body>
    <htmlpageheader name="myHeader1" style="display:none">
        <div style="text-align: right; border-bottom: 1px solid #000000; font-weight: bold; font-size: 10pt;">
            My document header
        </div>
    </htmlpageheader>

    <htmlpagefooter name="myFooter2" style="display:none">
        <table width="100%">
            <tr>
                <td width="33%">My document</td>
                <td width="33%" align="center">Page {PAGENO} of {nbpg}</td>
                <td width="33%" style="text-align: right;">{DATE Y-m-j}</td>
            </tr>
        </table>
    </htmlpagefooter>

EOF;

    return preg_replace($bodystring, $bodyrepl, $html);
}

$writer = new Mpdf($spreadsheet);
$writer->setEditHtmlCallback('addHeadersFooters');
$filename = 'issue.2236.pdf';
$writer->save($filename);
oleibman commented 1 year ago

Closing, no update in 11 months, no reason to think that the suggested code will not work.