1

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

现在,我想将这些 Excel 工作表转换为 PDF,因为我看到 PHPExcel 1.7.x 支持创建 PDF,所以我想:完美,我不必检查 OpenOffice 引擎,因为该引擎对于生成 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 生成的例子 mPDF 生成示例

4

1 回答 1

2

快速而肮脏的修复;

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

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

if (!$this->_useInlineCss) {
    $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
}
于 2013-06-10T13:23:28.750 回答