如何将Excel文件转换为pdf文件

时间:2019-01-09 07:18:21

标签: phpspreadsheet

如何将Excel文件转换为pdf文件。我安装了phpspreadsheet和tcpdf。但是我如何使phpspreadsheet使用Tcpdf。出现此错误

  

严重错误:找不到类“ TCPDF”   /home3/user1/public_html/site.com/downloads/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Pdf/Tcpdf.php   在第21行

这是我的代码

 require '../phpspreadsheet/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;





$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('testxl.xlsx');

$class = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf::class;
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', $class);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf');

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf($spreadsheet);
$writer->save("demo.pdf");

2 个答案:

答案 0 :(得分:0)

如果使用的是诸如codeigniter之类的框架,则需要使用composer或在config.php中使用$ config ['composer_autoload'] = [auto_config / autoload的路径]加载TCPDF类。

您还需要具有TCPDF库。你可以得到 https://github.com/tecnickcom/tcpdf

答案 1 :(得分:0)

您需要传递$ spreadsheet和writer类型到createWriter()。可以在phpspreadsheet中使用的pdf writer类型的示例是Dompdf,Mpdf和Tcpdf。

$writer     =\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Tcpdf');
//$writer   =\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Dompdf');
//$writer   =\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
$pdf_path   ='output/tcpdf_output.pdf'; 
$writer->save($pdf_path);