{"record":{"id":"b28926680445c149","repo":"PHPOffice/PhpSpreadsheet","slug":"directory-does-not-exist-temporarydirectory","errorCode":null,"errorMessage":"Directory does not exist: $temporaryDirectory","messagePattern":"Directory does not exist: \\$temporaryDirectory","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/Pdf.php","lineNumber":201,"sourceCode":"    /**\n     * Get temporary storage directory.\n     */\n    public function getTempDir(): string\n    {\n        return $this->tempDir;\n    }\n\n    /**\n     * Set temporary storage directory.\n     *\n     * @param string $temporaryDirectory Temporary storage directory\n     */\n    public function setTempDir(string $temporaryDirectory): self\n    {\n        if (is_dir($temporaryDirectory)) {\n            $this->tempDir = $temporaryDirectory;\n        } else {\n            throw new WriterException(\"Directory does not exist: $temporaryDirectory\");\n        }\n\n        return $this;\n    }\n\n    /**\n     * Save Spreadsheet to PDF file, pre-save.\n     *\n     * @param resource|string $filename Name of the file to save as\n     *\n     * @return resource\n     */\n    protected function prepareForSave($filename)\n    {\n        //  Open file\n        $this->openFileHandle($filename);\n\n        return $this->fileHandle;","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/Pdf.php#L183-L219","documentation":"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().","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n$writer = new \\PhpOffice\\PhpSpreadsheet\\Writer\\Pdf\\Mpdf($spreadsheet);\n$writer->setTempDir('/var/tmp/pdf-export'); // throws if missing\n\n// after\n$dir = '/var/tmp/pdf-export';\nif (!is_dir($dir)) {\n    mkdir($dir, 0775, true);\n}\n$writer->setTempDir($dir);","handlingStrategy":"validation","validationCode":"$tempDir = rtrim($config['pdf_temp_dir'], '/');\nif (!is_dir($tempDir)) {\n    if (!mkdir($tempDir, 0775, true) && !is_dir($tempDir)) {\n        throw new RuntimeException(\"Cannot create PDF temp dir: $tempDir\");\n    }\n}\nif (!is_writable($tempDir)) {\n    throw new RuntimeException(\"PDF temp dir not writable: $tempDir\");\n}\n$writer->setTempDir($tempDir);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["pdf","temp-dir","directory","configuration","mpdf","dompdf"],"backgroundTag":"directory-not-found","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}