PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

Unable to save to pdf

Open fakol opened this issue 2 years ago • 12 comments

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

<?php
require "../../vendor/config.php";
require "../../vendor/autoload.php";

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$counter = 0;
$helper->log('Populate spreadsheet');
for ($row = 1; $row < 501; ++$row) {
    $sheet->getCell("A$row")->setValue(++$counter);
    // Add many styles by using slight variations of font color for each.
    $sheet->getCell("A$row")->getStyle()->getFont()->getColor()->setRgb(sprintf('%06x', $counter));
    $sheet->getCell("B$row")->setValue(++$counter);
    $sheet->getCell("C$row")->setValue(++$counter);
}

$helper->log('Write to Mpdf');
IOFactory::registerWriter('Pdf', \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class);
$helper->write($spreadsheet, __FILE__, ['Pdf']);
$spreadsheet->disconnectWorksheets();

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

fakol avatar Jul 10 '23 10:07 fakol

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");

MarkBaker avatar Jul 10 '23 11:07 MarkBaker

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");

fakol avatar Jul 10 '23 11:07 fakol

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

MarkBaker avatar Jul 10 '23 12:07 MarkBaker

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 image

I made it so that the pictures appear, but the pdf still does not open

fakol avatar Jul 10 '23 13:07 fakol

At a guess, as the images look broken, your image path is wrong

MarkBaker avatar Jul 10 '23 15:07 MarkBaker

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

fakol avatar Jul 11 '23 09:07 fakol

Maybe you have an error in your code? image

fakol avatar Jul 11 '23 09:07 fakol

Uncaught Error: Class 'Mpdf\Mpdf' not found

That sounds like you don't have the mpdf library installed

MarkBaker avatar Jul 11 '23 09:07 MarkBaker

image

Or do I need to install something else?

fakol avatar Jul 11 '23 09:07 fakol

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.

MarkBaker avatar Jul 11 '23 09:07 MarkBaker

Where then is the correct information on the use of mPDF ? if they say use like this image

All the file has been created and is opening, thanks for the help

fakol avatar Jul 11 '23 09:07 fakol

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

MarkBaker avatar Jul 11 '23 11:07 MarkBaker

Problem seems resolved, no update in over a year, closing.

oleibman avatar Aug 18 '24 07:08 oleibman