Laravel routing: How to create a link to a PDF file
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Routing: How to Create a Link to a PDF File
As developers, we often deal with the need to present generated content—like downloadable documents—to users. When working within the Laravel framework, understanding how routing interacts with file serving is crucial. You are asking a very common question: how do I create a clean, accessible hyperlink (<a> tag) pointing directly to a PDF file stored in my application directory?
The short answer is that you generally need to separate the concern of routing (defining web endpoints) from the concern of file serving (making static assets available). Simply referencing a local path in an HTML link will fail unless Laravel is explicitly configured to handle that specific request.
This post will walk you through the proper, robust way to serve PDF files and create links to them within your Laravel application.
The Misconception: Routes vs. Static Files
You correctly noted that simply using a path like <a href="http://my.domain.org/assets/pdf/file.pdf">Link</a> often doesn't work because Laravel’s routing system, by default, expects requests to be handled by defined controller methods (routes). It doesn't automatically treat every file in your storage or public directory as a route endpoint.
When dealing with static assets like PDFs, the solution lies in leveraging Laravel’s dedicated public asset handling mechanism rather than trying to force it through a custom route for every single download.
Best Practice: Serving Files via the Public Directory
In a standard Laravel setup, any file placed inside the public directory is directly accessible via a URL. This is the intended way to serve assets like PDFs, images, and CSS files.
1. File Structure Setup
Ensure your PDF files are stored in the public folder. A common structure for documents might look like this:
your-laravel-app/
├── public/
│ ├── assets/
│ │ └── pdfs/
│ │ └── file.pdf <-- Your document is here!
│ └── index.php
├── app/
└── routes/
2. Creating the Link
If your files are correctly placed in public, you can create a direct link to them using the public URL prefix. This approach keeps your routing clean and allows the web server (Apache, Nginx) to handle the file delivery efficiently.
The HTML would look like this:
<!-- Assuming the PDF is at public/assets/pdfs/file.pdf -->
<a href="/assets/pdfs/file.pdf">Download PDF File</a>
When a user clicks this link, the browser requests /assets/pdfs/file.pdf, and because this path exists directly under your web server's document root (which Laravel exposes via the public folder), the file is served directly. This method bypasses complex route definition for simple downloads.
Advanced Scenario: Linking After Generation (The Controller Approach)
While linking to static files is easy, your initial requirement mentioned generating PDFs and sending them via email. For these more dynamic operations, you must use a route. The route's job is not just to serve the file, but to manage the logic (generating the PDF content, perhaps using a library like DomPDF or TCPDF) before delivering it.
Example Route Implementation
If you need a custom endpoint for downloading, define a route in routes/web.php:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PdfController;
// Define a route that maps to a controller method
Route::get('/download-pdf/{filename}', [PdfController::class, 'download'])->name('pdfs.download');
Controller Logic
The controller then handles reading the file from storage and forcing a download response:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
class PdfController extends Controller
{
public function download($filename)
{
// Check if the file exists in the configured storage (e.g., 'public' disk)
if (!Storage::disk('public')->exists('pdfs/' . $filename)) {
abort(404, 'PDF file not found.');
}
// Stream the file directly to the browser
return response()->download(
Storage::disk('public')->path('pdfs/' . $filename),
$filename
);
}
}
This method is superior for complex workflows because it ensures that security checks are performed, and you maintain centralized control over how files are accessed—a core principle of good application architecture, similar to the structure promoted by platforms like https://laravelcompany.com.
Conclusion
To summarize, when linking to PDF files in Laravel:
- For Static Assets: Place your PDFs in the
publicdirectory and link directly to the public URL path (e.g.,/assets/file.pdf). This is the simplest and most performant method for direct file access. - For Dynamic Generation: If you need to generate or process the PDF before delivery (like sending via email), define a dedicated route that points to a controller method. This gives you the necessary control over security, file handling, and business logic.
By distinguishing between serving static files and executing dynamic logic through routes, you ensure your application remains clean, scalable, and adheres to best practices in web development.