laravel dompdf get base64 format
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Delivering PDFs in Base64: Mastering laravel-dompdf for Frontend Consumption
As developers working within the Laravel ecosystem, generating dynamic documents is a common requirement. Tools like laravel-dompdf simplify the process of rendering HTML into PDF format. However, sometimes, simply forcing a file download isn't enough. Modern frontend frameworks, especially those leveraging API responses (like Vue components), often prefer receiving raw data directly in memory, such as Base64 encoded strings, for immediate display or embedding without triggering a file save dialog.
This post dives deep into the specific challenge of getting a generated PDF from Laravel to be served as a Data URI (data:application/pdf;base64,...) rather than a downloadable file. We will explore why the standard methods fail and demonstrate the correct, robust way to achieve this.
The Challenge: From Stream to String
When you use PDF::loadView(...) and call $pdf->stream(), DomPDF generates the PDF content and streams it directly to the HTTP response buffer. By default, Laravel handles this stream directly, resulting in a file download for the client.
The goal is to intercept this raw binary data stream, convert it into a Base64 string, and prepend the necessary MIME type prefix (data:application/pdf;base64,) so that the client (e.g., a Vue component) can render the PDF directly in the browser without needing to save a file.
The immediate attempt using base64_encode($pdf->stream()) often fails because PHP's stream functions read data sequentially, and trying to base64 encode an active stream pointer doesn't capture the entire buffered content correctly for HTTP responses.
The Solution: Capturing the Stream Content
To successfully implement this, we must explicitly capture the raw PDF content into a string buffer before encoding it. This involves using PHP stream functions to read the output generated by the PDF object. As we often deal with complex data handling in Laravel applications, ensuring clean separation of concerns—perhaps utilizing services or repositories, aligning with best practices seen in projects built on the principles of Laravel—is crucial for maintainability.
Here is the corrected and robust approach to achieve the Base64 output:
Implementation Steps in the Controller
We need to read the contents generated by $pdf->stream() into a variable, encode that variable, and then construct the final Data URI string for the response.
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;
class DocumentController extends Controller
{
public function generatePdfBase64()
{
// 1. Load the PDF using laravel-dompdf
$pdf = PDF::loadView('documentTemplate', [
'values' => $documentValues // Assuming documentValues is populated elsewhere
]);
// 2. Capture the PDF content into a string buffer
// We use stream_get_contents to read the raw output from the stream object.
$pdfContent = stream_get_contents($pdf->stream());
// 3. Encode the binary content to Base64
$base64Pdf = base64_encode($pdfContent);
// 4. Construct the Data URI response header
$dataUri = "data:application/pdf;base64," . $base64Pdf;
// 5. Return the response directly to the client
return response($dataUri, 200, [
'Content-Type' => 'application/pdf' // Important for browser interpretation
]);
}
}
Explanation of the Fix
The key difference here is explicitly calling stream_get_contents($pdf->stream()). This forces PHP to read the entire content generated by the DomPDF stream into the $pdfContent variable as a raw string, which can then be safely passed to base64_encode(). Finally, we wrap this encoded data with the standard Data URI scheme.
Conclusion
By understanding how output buffering works in PHP streams and explicitly capturing the content before encoding it, we can successfully transform a server-side PDF generation task into a highly usable Base64 format for modern frontend consumption. This technique allows your Vue component to ingest the file data directly as a string, enabling seamless embedding or display without relying on traditional file downloads. Always remember to prioritize clear data flow when building complex applications; think about how your services interact, much like designing clean relationships in Eloquent models when structuring data retrieval from the database via Laravel.