Search code examples
phppdfcakephpmpdfphpoffice

Cakephp: mpdf stores generated pdf in forbidden directory


In my Cakephp 3.6 application I am using mpdf to create pdf files. While on localhost is working without any problem, when I try it on the server I get this error :

SplFileInfo::isFile() [https://secure.php.net/splfileinfo.isfile'>splfileinfo.isfile]: open_basedir restriction in effect. File(/tmp/mysql.sock) is not within the allowed path(s):

Thats because it is trying to save the pdf file under src folder and not under webroot.

Here is the code: (excel generator works fine)

$inv = TableRegistry::get('invoices')->get($invoice->id);   
$inv->file_id = $newFile->id;
TableRegistry::get('invoices')->save($inv);
$writer = new Xlsx($spreadsheet);
$writer->save($folder->path.'/timologio'.$inv->invoice_no.'.xlsx');



//Save as Pdf, even though $folder->path is pointing under webroot, its trying to save it under src
$PdfWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Mpdf');
$PdfWriter->save($folder->path.'/timologio'.$inv->invoice_no.'.pdf');

If I change open_basedir from {WEBSPACEROOT}{/}{:}{TMP}{/} to none then it is working but is it safe to do so?


Solution

  • Set temporary directory for PHPSpreadsheet PDF writer to a location you have access to:

    $PdfWriter->setTempDir('path/to/your/temp/directory');
    $PdfWriter->save($folder->path.'/timologio'.$inv->invoice_no.'.pdf');
    

    As hinted at https://github.com/PHPOffice/PhpSpreadsheet/issues/1123#issuecomment-523361110