1

我有一个基于 PHPExcel 的项目,我想要两种输出类型:.xls 和 .pdf。.xls 输出效果很好,这没问题,但是只要我想将结果输出为 .pdf 文件,它就无法正常工作……最终的 .pdf 文件是 0B 文件,所以完全是空的。

这是文件开头的 PDF 编写器(tcPDF,位于 /var/www/tcpdf 目录中)初始化部分:

/** 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");
4

0 回答 0