mPDF temporary file is not writable using Yii

mPDF temporary file is not writable using Yii

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

mPDF Temporary File Is Not Writable Using Yii

When deploying a Yii application that generates PDFs with mPDF, you may encounter an error on your staging or production server stating that the temporary files directory is not writable. Specifically, mPDF throws a RuntimeException indicating that the path /var/www/protected/vendor/mpdf/mpdf/src/Config/../../tmp lacks write permissions. This issue typically arises when the web server process (such as Apache or Nginx running as www-data) does not have execute or write access to the specific directory mPDF expects to use for internal processing.

In a production environment, you cannot simply ignore this error because mPDF requires a writable temporary directory to function correctly, even if it uses the "Output inline" or "I" mode for immediate output. The library creates temporary files during the rendering process—often for font embedding or image handling—and fails if it cannot write them.

Understanding the Default Temporary Directory

By default, mPDF looks for a temporary directory relative to its installation location. If you do not explicitly configure one, it defaults to a path like /var/www/protected/vendor/mpdf/mpdf/src/Config/../../tmp. In many standard Linux distributions, this resolves to a directory inside your project's vendor folder or the global mPDF package location.

The critical issue is that web servers often run as unprivileged users (like www-data). While your local development machine might have broad permissions allowing any user to write anywhere, production servers enforce stricter security models. The default path usually points to a directory owned by root or another system user, causing the Permission denied error when the web server attempts to create files there.

Configuring a Custom Temporary Directory

The most robust solution is to explicitly tell mPDF where to look for temporary files and ensure that location exists and is writable by the web server process. You can achieve this by passing configuration options directly to the Mpdf constructor.

Instead of relying on the default path, specify an absolute path to a directory you control. For example, create a dedicated folder in your project root or within the tmp directory of your web root:

$mpdf = new \Mpdf\Mpdf([
    'mode' => 'utf-8', 
    'format' => 'A4-L',
    'tempDir' => '/path/to/writable/folder' // Absolute path recommended
]);

Setting the tempDir option overrides the default relative path calculation. Ensure that /path/to/writable/folder exists before initializing mPDF. If it does not exist, create it using a CLI command or a setup script during deployment:

sudo mkdir -p /var/www/html/tmp/mpdf-temp
sudo chown www-data:www-data /var/www/html/tmp/mpdf-temp
sudo chmod 755 /var/www/html/tmp/mpdf-temp

The chown command ensures the directory is owned by the web server user, and chmod 755 grants read, execute, and write permissions to the owner while allowing group and others to read and execute (traverse). This setup prevents the "not writable" error while maintaining reasonable security boundaries.

Security Considerations for Temporary Files

While creating a custom temporary directory resolves the immediate issue, it is important to consider security implications. Storing temporary files inside your vendor directory can be risky because that path is often outside the scope of application-level cleanup logic. If mPDF fails to clean up these files after generating a PDF, you could eventually hit disk space limits or leave sensitive data (like font caches) exposed in an unexpected location.

By moving the temporary directory to a standard tmp folder within your web root (e.g., /var/www/html/tmp/), you align with common practices where such directories are expected to be managed by the server environment. Additionally, ensure that this directory is not accessible via direct URL requests unless necessary, as mPDF temporary files are intended for internal use only.

If you are using Laravel or a similar framework, consider placing this directory in storage/framework/tmp and updating your web server configuration (like .htaccess or Nginx) to block public access to that specific subdirectory. This way, you maintain full control over the lifecycle of temporary files without compromising application security.

Our network