PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Writer\Exception
Directory does not exist: $temporaryDirectory
Error message
Directory does not exist: $temporaryDirectory
What it means
The PDF writers (mPDF/Dompdf/TCPDF wrappers) render HTML to an intermediate file in a temporary directory before producing the PDF. setTempDir() validates its argument and refuses anything that is not an existing directory, throwing this WriterException immediately rather than failing later during save().
Solutions
- Create the directory before setting it: if (!is_dir($dir)) { mkdir($dir, 0775, true); }
- Prefer sys_get_temp_dir() (always exists) over a custom path unless you must isolate storage.
- Resolve relative paths to absolute with realpath()/base_path() so cwd differences cannot break the check.
- Provision the directory in your deployment (Dockerfile RUN mkdir, ansible task) if it is fixed configuration.
Example fix
// before
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);
$writer->setTempDir('/var/tmp/pdf-export'); // throws if missing
// after
$dir = '/var/tmp/pdf-export';
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
$writer->setTempDir($dir); Defensive patterns
Strategy: validation
Validate before calling
$tempDir = rtrim($config['pdf_temp_dir'], '/');
if (!is_dir($tempDir)) {
if (!mkdir($tempDir, 0775, true) && !is_dir($tempDir)) {
throw new RuntimeException("Cannot create PDF temp dir: $tempDir");
}
}
if (!is_writable($tempDir)) {
throw new RuntimeException("PDF temp dir not writable: $tempDir");
}
$writer->setTempDir($tempDir); Prevention
- Default to sys_get_temp_dir() unless isolation is required.
- Resolve configured paths to absolute (realpath) before calling setTempDir to avoid cwd-dependent failures.
- Provision fixed temp directories in deployment artifacts (Dockerfile, IaC) alongside the config that names them.
- Add a boot-time health check that validates all configured directories exist and are writable.
When it happens
Trigger: Calling $writer->setTempDir($path) where $path does not exist: typo, a subdirectory never created, a relative path resolved from a different working directory (CLI vs web SAPI), or a path provisioned only on another environment.
Common situations: Config-driven temp directories (e.g. sys_get_temp_dir() . '/pdf') that work in dev but are never created in the deployed container; switching from absolute to relative paths; tmpwatch/systemd-tmpfiles cleaning the directory between deploys.
Related errors
- Chart renderer must implement…
- Directory does not exist: $cacheDirectory
- Invalid timezone
- Invalid value $calculateDateTimeType for calculated date…
- Line ending must be \n (Unix) or \r\n (Windows)
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/b28926680445c149.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpSpreadsheet/Writer/Pdf.php:201
/**
* Get temporary storage directory.
*/
public function getTempDir(): string
{
return $this->tempDir;
}
/**
* Set temporary storage directory.
*
* @param string $temporaryDirectory Temporary storage directory
*/
public function setTempDir(string $temporaryDirectory): self
{
if (is_dir($temporaryDirectory)) {
$this->tempDir = $temporaryDirectory;
} else {
throw new WriterException("Directory does not exist: $temporaryDirectory");
}
return $this;
}
/**
* Save Spreadsheet to PDF file, pre-save.
*
* @param resource|string $filename Name of the file to save as
*
* @return resource
*/
protected function prepareForSave($filename)
{
// Open file
$this->openFileHandle($filename);
return $this->fileHandle;View on GitHub (pinned to 65b080eef4)