PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.26k stars 2.69k forks source link

MPDF integration #2045

Open Pepita73 opened 3 years ago

Pepita73 commented 3 years ago

Is your feature request related to a problem? Please describe.

The mpdf package use self temp directory like vendor/mpdf/mpdf/tmp. Usually the vendor directories not writable for php (cli or web), only readable. So we get warning and empty pdf file. Temporary files directory "...vendor/mpdf/mpdf/src/Config/../../tmp" is not writable But PHPWord settings can store this configuration.

Describe the solution you'd like

change line here: https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Writer/PDF/MPDF.php#L56

return new $mPdfClass(['tempDir' => Settings::getTempDir()]);

Additional context

We use word template with header and footer section, but - if hacking the tempDir - these sections not transferred to the pdf file. If you can, please fix it also.

season886 commented 1 year ago

I also have the same problem, I don't know how to create MPDF constructor to pass parameters

Kozeke commented 1 year ago

I have same problem with MPDF, can you fix it, please?

kerstvo commented 1 year ago

You can create your own writer to save pdf from word

final class MyMPDF extends \PhpOffice\PhpWord\Writer\PDF\MPDF
{
    protected function createExternalWriterInstance()
    {
        $mPdfClass = $this->getMPdfClassName();

        return new $mPdfClass([
            'tempDir' => \PhpOffice\PhpWord\Settings::getTempDir(),
        ]);
    }

    private function getMPdfClassName(): string
    {
        if ($this->includeFile != null) {
            // MPDF version 5.*
            return '\mpdf';
        }

        // MPDF version > 6.*
        return '\Mpdf\Mpdf';
    }
}

and call it:

$phpWord = \PhpOffice\PhpWord\IOFactory::load($filePath);
Settings::setTempDir(sys_get_temp_dir());

$mpdfWriter = new MyMPDF($phpWord);
$mpdfWriter->save($filePath);