Passing variable from DOMPDF controller to view
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Passing Variables from Laravel Controller to DOMPDF: The Definitive Guide
Creating PDF documents directly from web applications using libraries like DOMPDF is a powerful feature for generating invoices, reports, and statements. However, bridging the gap between your server-side data (Laravel Controller) and the presentation layer (the View rendered by DOMPDF) often presents tricky variable passing issues. As a senior developer, I’ve seen many developers run into exactly this problem: correct data is available in the controller, but it seems lost when rendering a PDF.
This guide will walk you through the exact solution for passing complex variables from your Laravel Controller to your DOMPDF view successfully, ensuring your generated PDFs are accurate and complete.
## Understanding the Problem: Why Variables Disappear
You are attempting to pass an array of Eloquent models to `PDF::loadView()`. While this method is designed to load a view, the way you structure the data often needs refinement for nested or complex objects when using external PDF generation libraries.
Your attempt:
```php
$data = [$salesorder, $detailemployee, $detailservice];
$pdf = PDF::loadView('summary.invoice', $data);
```
The error `Undefined variable: salesorder` occurs because the way DOMPDF processes the data passed to `loadView()` expects variables to be directly accessible within the view context, or it struggles with deeply nested array structures when iterating over them implicitly. The reason `dd($data)` works is that PHP can see the variables in the controller scope; however, the rendering engine (DOMPDF) needs a clear mapping of what to display.
## The Solution: Structuring Data for DOMPDF
The most robust way to pass data to a view intended for PDF generation is to structure the data as a single, cohesive array or object that contains all necessary information in a predictable format. Instead of passing loosely grouped models, you should prepare exactly what the view expects.
Here is the corrected and recommended approach:
### Step 1: Prepare the Data in the Controller
Instead of passing an array of model objects, structure your data into a single payload that is easy for the view to consume. You can format this data by accessing the necessary attributes directly or ensuring the models are correctly serialized if needed (though generally, passing Eloquent models works best).
In your controller method, consolidate all required information into one array:
```php
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
class InvoiceController extends Controller
{
public function pdf(Request $request, $id)
{
// Fetch the necessary data
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
// Structure the data into a single, flat array for easy access in the view
$pdfData = [
'salesorder_details' => $salesorder,
'employee_details' => $detailemployee,
'service_details' => $detailservice,
];
// Pass the structured data to loadView
$pdf = Pdf::loadView('summary.invoice', $pdfData);
return $pdf->download('invoice.pdf');
}
}
```
### Step 2: Accessing Variables in the View
Now, inside your `summary.invoice` Blade file, you access these variables directly using the keys you defined in the controller. This explicitly tells the view where to find the data, eliminating ambiguity for DOMPDF.
**`resources/views/summary/invoice.blade.php`**
```html
Invoice PDF
Sales Order Invoice
{{-- Accessing the data using the keys defined in the controller --}}Sales Order Information
Order Number: {{ $salesorder_details->order_number ?? 'N/A' }}
Employee Details
Name: {{ $employee_details->name ?? 'N/A' }}
Service Items
-
@foreach ($service_details->items as $item)
- {{ $item->description }} - ${{ $item->amount }} @endforeach