不能使用mPDF和PHPExcel导出页眉/页脚和分页?

时间:2019-01-27 18:24:32

标签: php phpexcel mpdf

我将mPDF与PHPExcel一起使用,似乎页眉和页脚未从我的excel内容中导出。有办法解决吗?

如果没有,我是否可以在创建PDF之前重新创建/添加页眉和页脚?

如果没有,是否可以仅将页码添加到pdf中?

谢谢!

编辑//更多代码。请参见页眉/页脚部分:

PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);

$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load("excel.xlsx");

$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = dirname(__FILE__) . '/mpdf';

$objWorksheet = $objPHPExcel->getActiveSheet();

// Header and Footer
$title = "Title";
$sub_title = "Subtitle";
$sub_title = "Title";
$date_exported = date('Y-m-d H:i');

$objWorksheet
    ->getHeaderFooter()->setOddHeader('&L&14'.$title."\n&11".$sub_title."&R&10Exported with PHP\n" . $date_exported);
$objWorksheet
    ->getHeaderFooter()->setEvenHeader('&L&14'.$title."\n&11".$sub_title."&R&10Exported with PHP\n" . $date_exported);
$objWorksheet
    ->getHeaderFooter()->setOddFooter("&RPage &P of &N");
$objWorksheet
    ->getHeaderFooter()->setEvenFooter("&RPage &P of &N");

$excelRow = 1;
$excelCol = 0;
$columns = Array('Title 1','Title 2','Title 3');
foreach($columns as $column_title)
{
    $objWorksheet->setCellValueByColumnAndRow($excelCol, $excelRow, $column_title);
    $objWorksheet->getCellByColumnAndRow($excelCol,$excelRow)->getStyle()->applyFromArray(
                array(
                    'font' => array(
                        'bold' => true,
                    ),
                    'alignment' => array(
                        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
                    ),
                    'borders' => array(
                        'top' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => '000000')
                        ),
                        'bottom' => array(
                            'style' => PHPExcel_Style_Border::BORDER_THIN,
                            'color' => array('rgb' => '000000')
                        )
                    ),
                    'fill' => array(
                        'type' => PHPExcel_Style_Fill::FILL_SOLID,
                        'color' => array('rgb' => 'd9d9d9')
                    )
                )
           );
    $objWorksheet->getColumnDimensionByColumn($excelCol)->setAutoSize(true);
    $objWorksheet->getCellByColumnAndRow($excelCol,$excelRow)->getStyle()->getAlignment()->setWrapText(false);
    $excelCol++;
}

$objWorksheet->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objWorksheet->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER);
$objWorksheet->getPageSetup()->setFitToPage(true);
$objWorksheet->getPageSetup()->setFitToWidth(1);
$objWorksheet->getPageSetup()->setFitToHeight(0);
$objWorksheet->getPageMargins()->setTop(0.5);
$objWorksheet->getPageMargins()->setRight(0.7);
$objWorksheet->getPageMargins()->setBottom(0.5);
$objWorksheet->getPageMargins()->setLeft(0.75);

if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    )) {
    die(
        'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
        '<br />' .
        'at the top of this script as appropriate for your directory structure'
    );
}

header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="pdf.pdf"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('php://output');
exit;

0 个答案:

没有答案