{"record":{"id":"3fb0d3849944ad7c","repo":"PHPOffice/PHPWord","slug":"could-not-open-file-filename-for-writing","errorCode":null,"errorMessage":"Could not open file $filename for writing.","messagePattern":"Could not open file \\$filename for writing\\.","errorType":"exception","errorClass":"PhpOffice\\PhpWord\\Exception\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Writer/PDF/AbstractRenderer.php","lineNumber":195,"sourceCode":"        $this->orientation = $value;\n\n        return $this;\n    }\n\n    /**\n     * Save PhpWord to PDF file, pre-save.\n     *\n     * @param string $filename Name of the file to save as\n     *\n     * @return resource\n     */\n    protected function prepareForSave($filename = null)\n    {\n        $fileHandle = fopen($filename, 'wb');\n        // @codeCoverageIgnoreStart\n        // Can't find any test case. Uncomment when found.\n        if ($fileHandle === false) {\n            throw new Exception(\"Could not open file $filename for writing.\");\n        }\n        // @codeCoverageIgnoreEnd\n\n        return $fileHandle;\n    }\n\n    /**\n     * Save PhpWord to PDF file, post-save.\n     *\n     * @param resource $fileHandle\n     */\n    protected function restoreStateAfterSave($fileHandle): void\n    {\n        fclose($fileHandle);\n    }\n}\n","sourceCodeStart":177,"sourceCodeEnd":212,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Writer/PDF/AbstractRenderer.php#L177-L212","documentation":"prepareForSave opens the target $filename with fopen(..., 'wb') before rendering the PDF; a false return means the path is unwritable (bad directory, missing permission, disk error), so saving cannot proceed.","triggerScenarios":"Thrown at src/PhpWord/Writer/PDF/AbstractRenderer.php:195 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that the target directory exists and the PHP process has write permission on it.","Verify the filename/path is valid and the disk is not full or read-only.","Save to a temporary writable location and move the file afterward."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}