我正在使用PHPSpreadsheet库将生成的XLSX文件转换为PDF。 XLSX转换和下载工作正常,但是当我尝试将此XLSX转换为PDF时,我得到的是空PDF文件,当我尝试打开它时,它说Adobe Reader / Web Reader说“无法打开此文件” < / p>
<?php
require_once(APPPATH .'third_party/phpspreadsheet/vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Exception;
use \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', 'Date')
->setCellValue('B1', 'XXXXX')
->setCellValue('C1', 'YYYY')
->setCellValue('D1', 'ZZZZZ')
->setCellValue('E1', 'XXXXX')
->setCellValue('F1', 'YYYY')
->setCellValue('G1', 'ZZZZ')
->setCellValue('H1', 'ZZZZZ');
// code to fill in the data
$spreadsheet->getActiveSheet()->fromArray($data["result_set"],null,'A2');
$writer = new Xlsx($spreadsheet);
IOFactory::registerWriter("PDF", Dompdf::class);
$pdfwriter = IOFactory::createWriter($spreadsheet, 'PDF');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');
$writer->save('php://output');