I am building a web application with Laravel 11. I want the users to be able to download HTML code from the website and save it as a PDF file. Therefore, I used the ‘Barryvdh/laravel-dompdf’.
However, it didn’t do anything. I was not able to download/stream the PDF file, there was no error code on Laravel, on the browser console, there was no logs, literally nothing happened.
So I thought I would create a new one laravel project and test it there. Upon trying it in the most basic form (please see the provided codes below). It literally does nothing when I go to the route that is supposed to call the function containing the code that would download the PDF file.
Here is a video of what happens if I go to the route: https://youtu.be/8sjwyLA-GTo
Here is the most basic form that I used to test the Barryvdh/laravel-dompdf that I found on YouTube:
routes/web.php
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersHtmltoPDFController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/testpdf',[HtmltoPDFController::class, 'htmltopdf'])->name('testpdf');
app/http/Controllers/HtmltoPDFController.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Pdf;
class HtmltoPDFController extends Controller
{
public function htmltopdf () {
$pdf = Pdf::loadView('pdf');
return $pdf->download('test.pdf');
}
}
resources/views/pdf.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1> Demo </h1>
</body>
</html>
Tldr: Used Barryvdh/laravel-dompdf on Laravel 11 to enable user download or stream HTML codes into PDF files. But nothing happens when I go to the route. No error code, no console error, no logs in the laravel.log, nothing.
Dummy Pingul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
10