Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to save to pdf #3643

Closed
fakol opened this issue Jul 10, 2023 · 13 comments
Closed

Unable to save to pdf #3643

fakol opened this issue Jul 10, 2023 · 13 comments

Comments

@fakol
Copy link

fakol commented Jul 10, 2023

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
Copy link
Member

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
Copy link
Author

fakol commented Jul 10, 2023

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
Copy link
Member

MarkBaker commented Jul 10, 2023

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
Copy link
Author

fakol commented Jul 10, 2023

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
Copy link
Member

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

@fakol
Copy link
Author

fakol commented Jul 11, 2023

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
Copy link
Author

fakol commented Jul 11, 2023

Maybe you have an error in your code?
image

@MarkBaker
Copy link
Member

Uncaught Error: Class 'Mpdf\Mpdf' not found

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

@fakol
Copy link
Author

fakol commented Jul 11, 2023

image

Or do I need to install something else?

@MarkBaker
Copy link
Member

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
Copy link
Author

fakol commented Jul 11, 2023

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
Copy link
Member

MarkBaker commented Jul 11, 2023

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants