PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.35k stars 3.47k forks source link

Unable to save to pdf #3643

Closed fakol closed 2 months ago

fakol commented 1 year ago

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

MarkBaker commented 1 year 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");
fakol commented 1 year ago

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");
MarkBaker commented 1 year ago

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

fakol commented 1 year ago

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

MarkBaker commented 1 year ago

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

fakol commented 1 year ago

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 commented 1 year ago

Maybe you have an error in your code? image

MarkBaker commented 1 year ago
Uncaught Error: Class 'Mpdf\Mpdf' not found

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

fakol commented 1 year ago

image

Or do I need to install something else?

MarkBaker commented 1 year ago

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.

fakol commented 1 year ago

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

MarkBaker commented 1 year ago

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

oleibman commented 2 months ago

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