Documentation

Permission Denied Whilst Generating PDFs

Craft Commerce makes use of dompdf to generate invoices for your customers. In some circumstances this can result in dompdf attempting to write temporary files into the vendor folder. This is generally bad practice and on Servd results in a permissions error being thrown as the vendor folder is owned by root.

We can resolve this by telling dompdf to use Craft's storage folder for its temporary files:

Event::on(
  Pdfs::class,
  Pdfs::EVENT_MODIFY_RENDER_OPTIONS,
  function (PdfRenderOptionsEvent $event) 
{
  $pathService = Craft::$app->getPath();
  $dompdfRootDir = $pathService->getTempPath() . DIRECTORY_SEPARATOR . 'commerce_dompdf';
  $dompdfFontDir = $pathService->getTempPath() . DIRECTORY_SEPARATOR . 'commerce_dompdf';
  $event->options->setRootDir($dompdfRootDir);
  $event->options->setFontDir($dompdfFontDir);
});

You can add this to a custom module's init function in your Craft project to ensure it runs with every request.