Closed fakol closed 2 months ago
Rather than using the helper, which is there as an abstraction to simplify the sample code, use the actual Writers as described in the documentation. We do provide the documentation for a reason.
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf($spreadsheet);
$writer->save("Save_Path/FileName_that_I_Want_to_Use_for_the_Saved_File.pdf");
I already understood how to create a file, thanks, but it somehow saves it wrong even when I try to save it simply to the server when opening the file, it gives a reading error
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setCreator('Maarten Balliauw')
->setLastModifiedBy('Maarten Balliauw')
->setTitle('Office 2007 XLSX Test Document')
->setSubject('Office 2007 XLSX Test Document')
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Test result file');
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Товары');
$sheet->setCellValue('A1', 'что-то тестовое');
$sheet->getStyle('A1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
$sheet->getStyle('A1')->getFill()->getStartColor()->setARGB('FFFF0000');
$sheet->mergeCells('A1:H1');
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$j = 2;
$i = 3;
while($list = mysqli_fetch_assoc($table)) {
$sheet->setCellValue('A'.$i, $list["tov_name"]);
$sheet->setCellValue('B'.$i, $list["tov_desc"]);
$drawing = new Drawing();
$drawing->setName($list["tov_name"]);
$drawing->setDescription($list["tov_desc"]);
$drawing->setPath('../uploads/'.$list["tov_img"]);
$drawing->setHeight(36);
$drawing->setCoordinates('C'.$j);
$drawing->setOffsetX(5);
$drawing->setOffsetY(5);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$sheet->getStyle('A'.$j)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$sheet->getStyle('B'.$j)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$sheet->getStyle('A'.$j)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->getStyle('B'.$j)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$sheet->mergeCells('A'.$j.':A'.$i, Worksheet::MERGE_CELL_CONTENT_MERGE);
$sheet->mergeCells('B'.$j.':B'.$i, Worksheet::MERGE_CELL_CONTENT_MERGE);
$sheet->mergeCells('C'.$j.':C'.$i);
$sheet->getColumnDimension('B')->setAutoSize(true);
$sheet->getColumnDimension('C')->setAutoSize(true);
$i+=2;
$j+=2;
}
$myWorkSheet = $spreadsheet->createSheet();
$myWorkSheet->setTitle('Образец');
$myWorkSheet->setCellValue('A1', 'что-то тестовое');
$myWorkSheet->getStyle('A1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
$myWorkSheet->getStyle('A1')->getFill()->getStartColor()->setARGB('FFFF0000');
$myWorkSheet->mergeCells('A1:H1');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->save("php://output");
That doesn't really help to diagnose any problem.
The PDF Writers all work the same way, they generate an HTML file, and then use the relevant PDF library functions to convert that HTML to PDF. Can you use the HTML Writer to generate an html file and see what that looks like?
Or try opening the generated PDF file in a text editor, and see if there is any obvious problem in there, such as a PHP error message
Here's what happened If you say that it creates just html content, then maybe the error is due to pictures that for some reason did not insert
I made it so that the pictures appear, but the pdf still does not open
At a guess, as the images look broken, your image path is wrong
I fixed it, but the pdf still won't open
When I try to save to the server (localhost), I get this error, maybe that's why it creates a broken pdf file? Because even with this error, a pdf file is created, but it also does not open
Fatal error: Uncaught Error: Class 'Mpdf\Mpdf' not found in D:\OpenServer\domains\localhost\clips\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Pdf\Mpdf.php:23 Stack trace: #0 D:\OpenServer\domains\localhost\clips\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Pdf\Mpdf.php(44): PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf->createExternalWriterInstance() #1 D:\OpenServer\domains\localhost\clips\admin\html\exportPDF.php(76): PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf->save() #2 {main} thrown in D:\OpenServer\domains\localhost\clips\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Pdf\Mpdf.php on line 23
Maybe you have an error in your code?
Uncaught Error: Class 'Mpdf\Mpdf' not found
That sounds like you don't have the mpdf library installed
Or do I need to install something else?
No! That is not an error in our code. As per the documentation, you need to install the mpdf library; that line of code is instantiating the class from that external library... external meaning that it isn't included as part of PhpSpreadsheet.
Where then is the correct information on the use of mPDF ? if they say use like this
All the file has been created and is opening, thanks for the help
Install the mpdf library using composer, then use the PhpSpreadsheet documentation to send your spreadsheet to PDF
The Writer\Pdf\Mpdf.php wrapper handles all the generation of the HTML, and sending it to the Mpdf library
Problem seems resolved, no update in over a year, closing.
Which versions of PhpSpreadsheet and PHP are affected?
1.28 \ 7.4
hello, how can i save the pdf file ? I use your code but it doesn't work for some reason
I looked at examples like this samples I looked through the files from the first to the sixth, but did not understand how to save the data in pdf