PHPExcel返回空PDF文件

时间:2013-01-11 10:47:05

标签: php html phpexcel

我有一个基于PHPExcel的项目,我希望有两种输出类型:.xls和.pdf。 .xls输出工作得很好,没有问题,但是一旦我想将结果输出为.pdf文件,它就不能正常工作......最终的.pdf文件是一个0B文件,所以总是空的。 / p>

以下是文件开头的PDF编写器(tcPDF,位于/ var / www / tcpdf目录中)inicializaion部分:

/** Include PHPExcel */
require_once 'scripts/PHPExcel.php';

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

/** PHPExcel_IOFactory */
include 'scripts/PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();

$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    )) {
    die(
        'Please set the $rendererName and $rendererLibraryPath values' .
        PHP_EOL .
        ' as appropriate for your directory structure'
    );
}

这里有操作函数,所以跳过这些......

这是输出部分:

$objPHPExcel->setActiveSheetIndex(0);

if($_GET['format'] == 'xls'){

    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="bs-export.xls"');

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

if($_GET['format'] == 'pdf'){

    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment;filename="bs-export.pdf"');

    $objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'PDF'); 
    $objWriter->setSheetIndex(0);
    $objWriter->save('php://output');
    exit;
}

有人可以帮我解决这个问题吗?

非常感谢!!

编辑:我没有注意到它有一些变量名称错误...所以我更正了

$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'PDF'); 

行到:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 

此后PDF文件不再是0B网站,但它仍然不是空白页...... XLS输出没问题。

EDIT2:很奇怪......这也会返回一个空白页:

$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->save("pdf-test.htm");

0 个答案:

没有答案