Not able to set fitToPage() with domPDF/tcPDF or landscape orientation with mPDF

Topics: Developer Forum, User Forum
May 29, 2013 at 7:01 AM
Hi. I’m using PHPExcel 1.7.8, and trying to get export of my spreadsheets to PDF to work.

The problem with the spreadsheets I generate is that they are too wide to fit onto a page, even if it’s in landscape mode. I was hoping to resolve this by trying to use setFitPage(true) or setScale(), but somehow those two methods never have any effect on the PDF I export. Scale never changes and the content never gets adjusted to the width of the page. Other page setup methods, like setOrientation() and setPaperSize() work, but only with domPDF or tcPDF rendering engines. With mPDF, the content is always fit to width, even without the setFitPage(true) (which is also weird), but the main problem is that with this engine, the page orientation is ignored (it’s always portrait).

So it’s extremely frustrating on the overall, and I was hoping to get some help with getting it to work here.

Here is what I use to export the PDF.
    private function outputPDF() {
        
        $this->PHPExcel->getActiveSheet()->setShowGridLines(false);
        $this->PHPExcel->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $this->PHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $this->PHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
        
        \PHPExcel_Settings::setPdfRenderer(\PHPExcel_Settings::PDF_RENDERER_MPDF, LITHIUM_LIBRARY_PATH . '/mpdf');
        // \PHPExcel_Settings::setPdfRenderer(\PHPExcel_Settings::PDF_RENDERER_DOMPDF, LITHIUM_LIBRARY_PATH . '/dompdf');
        // \PHPExcel_Settings::setPdfRenderer(\PHPExcel_Settings::PDF_RENDERER_TCPDF, LITHIUM_LIBRARY_PATH . '/tcpdf');
                
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment;filename="' . $this->learner['fullName'] . ' Report.pdf"');
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($this->PHPExcel, 'PDF');
        $objWriter->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); // even setting this doesn’t affect the way mPDF orients the page
        $objWriter->save('php://output');
        die;
        
    }
Coordinator
May 29, 2013 at 7:31 AM
Edited May 29, 2013 at 7:32 AM
The only page setup attributes that currently work with PDF are paper size and orientation, because these are the only attributes fed to the PDF rendering engine.... all other page attributes are Excel2007 and Excel5 only.

Note that all renderers should work with both paper size and page orientation with the latest github develop branch code.