how to save dompdf file to storage and name the file dynamicly in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Save DomPDF Files Dynamically in Laravel: Solving the File Saving Error
As a senior developer working with Laravel, generating dynamic content like PDFs is a common task. When you combine a library like DomPDF with your backend logic, saving the generated file correctly—especially when dealing with dynamic filenames—can often lead to frustrating errors like `failed to open stream: No such file or directory`.
This post will dive into why this error occurs and provide robust, modern solutions for dynamically saving PDF files in your Laravel application. We will look at both the immediate fix for your current setup and the recommended best practice using Laravel's built-in filesystem capabilities.
## Understanding the Error: Why `failed to open stream` Happens
The error `failed to open stream: No such file or directory` when using functions like `file_put_contents()` almost always points to an issue with the path you are attempting to write to, not necessarily an issue with DomPDF itself.
In your example code, which attempts to save the file here:
```php
file_put_contents('public/bills/'.$noOrder.'.pdf', $pdf->output());
```
There are two primary reasons this fails:
1. **Directory Existence:** The most common cause is that the directory structure leading up to the filename does not exist. If the `public/bills/` folder has not been created yet, PHP cannot create the file inside it, resulting in a stream error.
2. **Permissions:** Less commonly, the web server process (e.g., Apache or Nginx user) might lack the necessary write permissions for the `public` directory, preventing the operation from succeeding.
While using static names like `'public/bills/bubla.pdf'` might seem simpler, it still requires you to manually ensure that the parent directories (`public/bills/`) exist before writing the file, which introduces fragility when dealing with dynamic variables.
## Solution 1: The Immediate Fix – Ensuring Directory Existence
If you insist on using raw PHP file operations, you must explicitly check for and create the necessary directory structure before attempting to write the file. This makes your code more resilient against initialization errors.
You can use `mkdir()` with the recursive option (`true`) to ensure that all necessary parent directories are created if they don't exist:
```php
use Illuminate\Support\Facades\File; // Good practice to use Laravel facades
// ... inside your controller method
$fileName = 'public/bills/' . $noOrder . '.pdf';
$directory = dirname($fileName);
// Ensure the directory exists
if (!File::exists($directory)) {
File::makeDirectory($directory, 0755, true); // Create directories recursively
}
// Now safely write the file
file_put_contents($fileName, $pdf->output());
return redirect()->route('pesananIndex');
```
While this solves the immediate error, it is generally considered an older, less idiomatic approach within a modern Laravel application.
## Solution 2: The Laravel Best Practice – Using the Filesystem Facade
The most robust and scalable way to handle file operations in Laravel is by utilizing the `Illuminate\Support\Facades\Storage` facade. This method abstracts away the underlying operating system details, handles permissions automatically, and allows you to easily choose where your files are