PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

MPDF integration

Open Pepita73 opened this issue 4 years ago • 3 comments

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.

Pepita73 avatar Mar 22 '21 14:03 Pepita73

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

season886 avatar Mar 28 '23 10:03 season886

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

Kozeke avatar Apr 13 '23 11:04 Kozeke

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

kerstvo avatar Apr 13 '23 17:04 kerstvo