PHPExcel - PDF转换不起作用

时间:2014-04-17 16:26:51

标签: php phpexcel

我正在尝试打开.xls文件,通过PHPExcel进行编辑并保存为.pdf。我有这个源代码:

<?php   
set_include_path('Classes/');
require_once 'PHPExcel/IOFactory.php';
require_once 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load('document.xls');
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'řčřč');
$sheet->setCellValue('B1', 'B');
$sheet->setCellValue('C1', 'C');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('document.pdf');
?>

但它给了我这个错误

  

未捕获的异常'PHPExcel_Writer_Exception',消息为'PDF   渲染库尚未定义。在   /data/web/virtuals/55146/virtual/www/faktury/Classes/PHPExcel/Writer/PDF.php:56   堆栈跟踪:#0   /data/web/virtuals/55146/virtual/www/faktury/Classes/PHPExcel/IOFactory.php(141):   PHPExcel_Writer_PDF-&gt; __ construct(Object(PHPExcel))#1   /data/web/virtuals/55146/virtual/www/faktury/faktura.php(29):   PHPExcel_IOFactory :: createWriter(Object(PHPExcel),'PDF')#2 {main}   投入   /data/web/virtuals/55146/virtual/www/faktury/Classes/PHPExcel/Writer/PDF.php   在第56行

我的PHP程序与Classes文件夹位于同一文件夹中。

你有没有人知道?

非常感谢。

1 个答案:

答案 0 :(得分:0)

PHPExcel允许您选择使用DomPDF,TcPDF或mPDF,但您需要告诉PHPExcel您已经安装了这三个中的哪一个。

只需在脚本中设置一些变量即可获得帮助(因为您已经完成了),您需要告知 PHPExcel使用DomPDF以及DomPDF库所在的位置在你的系统中

<?php   
set_include_path('Classes/');
require_once 'PHPExcel/IOFactory.php';
require_once 'PHPExcel.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = dirname(__FILE__). 'libs/classes/dompdf' . $rendererLibrary;

//  Here's the magic: you __tell__ PHPExcel what rendering engine to use
//     and where the library is located in your filesystem
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'
    );
}

$objPHPExcel = new PHPExcel();
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load('document.xls');
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'řčřč');
$sheet->setCellValue('B1', 'B');
$sheet->setCellValue('C1', 'C');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('document.pdf');
?>