使用PHPExcel生成PDF失败

时间:2013-06-09 15:47:22

标签: pdf-generation phpexcel tcpdf mpdf

我通过加载Excel工作表生成Excel报表,我用Excel生成并用PHP代码填写一些数据。

现在,我想将这些Excel工作表转换为PDF,因为我看到PHPExcel 1.7.x支持创建PDF我认为:完美,我不必查看OpenOffice Engine,因为该引擎对于为Web用户实时生成PDF。

但是:除此之外,我没有机会使用PHPExcel中的PDF编写器来编写加载的Excel模板。甚至无法加载和编写传送的xls文件。

我试图仅通过PHP代码构建excel并删除excel模板:excel-to-pdf已经失败,最简单的生成的excel合并了两个单元格。 这位作家完全无法完成这项任务吗? 附录,2013-06-09:对不起,这可能是错误的:这可能是我的全部错,或者我只是不理解核心原则。

如果要输出pdf,以下简单的Excel生成代码将失败:

<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once 'app/_includes/PHPExcel_1.7/PHPExcel.php';

$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = '/inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/'. $rendererLibrary;
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'
    );
}
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Company X") 
                 ->setDescription("Test document for PDF, generated using PHP classes.");
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);

// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Date')
            ->setCellValue('B1', 'Time')
            ->setCellValue('B2', 'Start')
            ->setCellValue('C2', 'End');
// This line generated errors inside the output pdf -> corrupts PDF
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('B1:C1');

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(14);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(4);

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()->setShowGridLines(false);

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client.s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="p1.pdf"');
header('Cache-Control: max-age=0');

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

附录,2013-06-09:这是生成的PDF文件的开头:

Notice: Array to string conversion in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1236

Call Stack:
    0.0010     257896   1. {main}() /inet/xxx/HTTP/devel/p1.php:0
    0.0243    7798872   2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799224   3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799728   4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0243    7800064   5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0309    8678376   6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
    0.0313    8683176   7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436


Warning: Illegal string offset 'width' in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1257

Call Stack:
    0.0010     257896   1. {main}() /inet/xxx/HTTP/devel/p1.php:0
    0.0243    7798872   2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799224   3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799728   4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0243    7800064   5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0309    8678376   6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
    0.0313    8683176   7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436


Warning: Invalid argument supplied for foreach() in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1315

Call Stack:
    0.0010     257896   1. {main}() /inet/xxx/HTTP/devel/p1.php:0
    0.0243    7798872   2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799224   3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/p1.php:62
    0.0243    7799728   4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0243    7800064   5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87
    0.0309    8678376   6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119
    0.0313    8683176   7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436
    0.0320    8701296   8. PHPExcel_Writer_HTML->_assembleCSS() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:1267

%PDF-1.7

附录,2013-06-10:我尝试了使用tcpdf的21pdf.php示例。它向我显示了一些警告,但生成了pdf。这些是php输出:

21:55:52 Create new PHPExcel object
21:55:52 Set document properties
21:55:52 Add some data
21:55:52 Add comments
21:55:52 Add rich-text string
21:55:52 Merge cells
21:55:52 Protect cells
21:55:52 Set cell number formats
21:55:52 Set column widths
21:55:52 Set fonts
21:55:52 Set alignments
21:55:52 Set thin black border outline around column
21:55:52 Set thick brown border outline around Total
21:55:52 Set fills
21:55:52 Set style for header row using alternative method
21:55:52 Unprotect a cell
21:55:52 Add a hyperlink to the sheet
21:55:52 Add a drawing to the worksheet
21:55:52 Add a drawing to the worksheet
21:55:52 Add a drawing to the worksheet
21:55:52 Play around with inserting and removing rows and columns
21:55:52 Set header/footer
21:55:52 Set page orientation and size
21:55:52 Rename first worksheet
21:55:52 Create a second Worksheet object
21:55:52 Add some data
21:55:52 Set the worksheet tab color
21:55:52 Set alignments
21:55:52 Set column widths
21:55:52 Set fonts
21:55:52 Add a drawing to the worksheet
21:55:52 Set page orientation and size
21:55:52 Rename second worksheet
21:55:52 Hide grid lines
21:55:52 Set orientation to landscape
21:55:52 Write to PDF format using tcPDF
Notice: Array to string conversion in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1236 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 Warning: Illegal string offset 'width' in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1257 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 Warning: Invalid argument supplied for foreach() in /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php on line 1315 Call Stack: 0.0010 258104 1. {main}() /inet/xxx/HTTP/devel/21pdf.php:0 0.1375 8979304 2. PHPExcel_Writer_PDF->save() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8979656 3. PHPExcel_Writer_PDF->__call() /inet/xxx/HTTP/devel/21pdf.php:80 0.1375 8980096 4. call_user_func_array() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1375 8980432 5. PHPExcel_Writer_PDF_tcPDF->save() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF.php:87 0.1569 9542416 6. PHPExcel_Writer_HTML->generateSheetData() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/PDF/tcPDF.php:119 0.2014 9683184 7. PHPExcel_Writer_HTML->_generateRow() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:436 0.2019 9685336 8. PHPExcel_Writer_HTML->_assembleCSS() /inet/xxx/HTTP/devel/app/_includes/PHPExcel_1.7/PHPExcel/Writer/HTML.php:1267 21:55:53 File written to 21pdf_tcPDF.pdf
Call time to write Workbook was 0.9275 seconds
21:55:53 Current memory usage: 16 MB
21:55:53 Peak memory usage: 18 MB
21:55:53 Done writing files
File has been created in /inet/xxx/HTTP/devel

有趣的旁注:看一下tcpdf和mPDF的不同输出。我想我现在知道了,为什么你把PDF生成器的选择留给用户。 tcpdf generated example mPDF generated example

1 个答案:

答案 0 :(得分:2)

快速而肮脏的修复;

更改PHPExcel / Writer / HTML.php的第1236行,其内容为:

$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();

if (!$this->_useInlineCss) {
    $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
}